Map JSONObject to SQL server - java

I know the title is quite cliched, but it is not about storing JSON data in SQL Server.
I have a JSONArray with JSONObjects with keys that match the column names in SQL Server 2012. I want to save the data to the database into the proper columns.
I know the obvious way to do this is the iterate through the JSONArray and save the values with individual insert commands. I was wondering if there was another way to do this.
I don't want to use T-SQL. I want to handle this from Java only.
Here is an example data that matches the format of my JSONArray:
[
{
"FEATURE":"A",
"OPTION":"92384",
"ERROR_TYPE":"MISSING",
"DESCRIPTION":"Feature A is missing the option 92384",
"SERIAL_NUMBER":"249752-23894"
},
{
"FEATURE":"B",
"OPTION":"0288394",
"ERROR_TYPE":"MISSING",
"DESCRIPTION":"Feature B is missing the option 0288394",
"SERIAL_NUMBER":"Y2394-20392Q"
}
]
My SQLServer table looks like this:
What would be best way to achieve this without looping through each JSONArray?

As you have added java tag I would convert JSON to Java object and save it with Hibernate. Here are two useful links how to do that
Json to Java
Hibernate example

DECLARE #testJson NVARCHAR(4000)= N'[{"id":2,"name":"n2"},{"id":1,"name":"n1"}]'
INSERT
INTO
test_table SELECT
*
FROM
OPENJSON(#testJson) WITH (
id int N'$.id',
name VARCHAR(200) N'$.name'
)
Update
use java
jsonStr -> jsonObject and getKeys (some json libs )
String youJson = "{\"id\":0, \"name\":\"n0\"}";
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(json).getAsJsonObject();
Set<String> keys = jsonObject.keySet();
Then use NamedParameterJdbcTemplate and MapSqlParameterSource (spring-jdbc) to create sql and set parameters dynamically .
PS. I hate ORM in java.

Related

Migrate postgresql JSON array of IDs to Mongodb ObjectID

I'm trying migrate from postgres db to mongodb. I've a field JSON in postgres table, which has key and value pairs. I need to fetch the value based on JSON key and convert those values(values are basically postgres id) to Mongo objectID and then map those to mongo document.
JSON field looks like this
"B":["956c5b0a5341d14c23ceed071bf136f8"]}
I've written a function in java to convert postgres ID column to mongoID.
public String convertIDToMongo(String colName){
---encoding logic
}
This is working when the field is explicitly available in the table and also if the field datatype is not an array. In case of JSON and array, is there a way to fetch set of values from JSON field and convert them into Mongo Object ID?
"Select json::json->'A' from table" ```
gives me the value ["06992a6fef0bcfc1ede998ac7da8f08b","7d1d55e1078191e53244c41d6b55b3a3","31571835c6600517f4e4c15b5144959b"] But I need some help to convert this list of IDs to Mongo Object IDs. Below is how I convert for non JSON field with one postgres ID value to Mongo ID.
"Select " + convertIDToMongo('colName') + " as "_id", \n" +
Any help is greatly appreciated. Thanks.
You need to parse the JSON array, then call your converter for each item in the array. There are many JSON parsing options available.

Converting JSON to entities and storing in mongoDB using morphia

I have a JSON string being sent from a client (browser ).I want to save it to my mongoDB database which already has some collections defined by the user.I was able to successfully save objects using Morphia.But How can I do the same if I already have the JSON string being returned from client I want to put in the "bands" collection.
Mongo mongo = new Mongo("localhost");
Datastore datastore = new Morphia().createDatastore(mongo,
"bandmanager");
Band band = new Band();
band.setName("Punjabi band");
band.getMembers().add("Lucky1");
band.getMembers().add("Lucky2");
band.getMembers().add("Lucky3");
band.getMembers().add("Lucky4");
band.getMembers().add("Lucky5");
band.getMembers().add("Lucky6");
band.setGenre("Punjabi");
datastore.save(band);
Did you annotate Band with #Entity("bands")? I'm not sure what you're asking... Are you asking how to convert that json string in to a Band object? If so, look in to jackson
If you already have a JSON object, you don't really need Morphia. You can simply do the following with the Java driver:
DBObject dbObject = (DBObject) JSON.parse(yourJsonString);
For a full blog post on this see http://www.mkyong.com/mongodb/java-mongodb-convert-json-data-to-dbobject/
PS: Do not forget to sanitize the JSON you get from the client!

How can I bulk update my mongo data to add a fixed level?

I recently changed a POJO from having all its typed properties to something free in a typed JSONObject field called content.
The problem is that all old documents map to the old POJO version, so they are stored like this:
{"_id":"ObjectId(value)","field1":"value1","field2":"value2"}
Can I update all fields via a single mongo command so I can wrap all the content, except the id, so the result would be something like this:
{"_id":"ObjectId(value)","content":{"field1":"value1","field2":"value2"}}
?
Or should I program a simple program that does it one by one? (as in iterating all values sort of manually adding the new content level)
Unfortunately, there are no MongoDB commands that will allow you to restructure a document in this way. You'll need to write a program to fetch all of your documents one by one, update the structure, and then send the updated structure back to MongoDB.
Often the best way to do this is to write the modified documents to a new collection, and then drop the old collection when you're done.
I solved it creating a .js file to execute via mongo shell.
mongo myDb fixresults.js
The file is as follows:
for( var c = db.results.find(); c.hasNext(); ) {
var full = c.next();
var anon = db.results.findOne({"_id":full._id},{"_id":0});
var n = {"_id":full._id,"content":anon};
db.results.temp.insert(n);
}
This will insert the transformed value into the .temp collection, which you can rename later to replace the original.

How to save the JSON data to the database

I am trying to save the JSON response of a website to the database. Latter I am accessing this JSON string for processing the data. But I am not able to parse it as JSON object. On analyzing I realize that the characters of strings needs to be escaped. Since I am saving the data as a string the data results which am getting back as a JSON data are not escaped in the database. Is there a way out how I can save the JSON data to the database.
Example:
{"RULE":[{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src="[^"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\s*</a>","type":"text"}},{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src="[^"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\s*</a>","type":"text"}}]}
Want to save in database as (also getting the response as) i.e. the " and \ are escaped
{"RULE":[{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src=\"[^\"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\\s*</a>","type":"text"}},{"replace":{"value":"","type":"text"},"match":{"value":"<a [^>]*><img src=\"[^\"]*WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites[^>]*>\\s*</a>","type":"text"}}]}
Here is the code I have used to save the data in the database
// here the raw_data is the data from the website
JSONObject jo = new JSONObject(raw_data);
// Get the JSONObject value associated with the search result key.
jo = jo.getJSONObject("pipe");
jo = jo.getJSONObject("definition");
String def=jo.toString();
JSONArray jo1=jo.getJSONArray("modules");
JSONArray jo2=jo.getJSONArray("wires");
/*
* Write the contents in the data base
*
*
*/
def =def.replaceAll( "[']", "\\\\\'" ); //creates problem for strings with '
def =def.replaceAll( "&", "%26" );
String tablename="PipesTable2";
System.out.println(def);
database d=new database();
I suggest you to create an class having properties mapped to json. You can convert object to json or json to object using third party tool.you can use Gson.
Now what you can do when you get the josn you can convert this json to object and save that object's properties to database. when you retrieve the value, set these value to object's properties and then convert this object to json.
You can find Gson here -
http://code.google.com/p/google-gson/
This is easy to use.
Have you considered using a library like Jackson ( http://wiki.fasterxml.com/JacksonHome ) to translate between Json objects and Java objects? Then you can use standard Java persistence tools and libraries to save it out to/read it in from your database, and it will handle all your escaping automatically.
Jackson has two main ways that it translates between Json and Java objects. If you're planning on using direct JDBC calls for your persistence and you don't want to do much other processing, then you can get away with "Simple Data Binding". This just translates between Json and Java's built-in types (List, Map, String, Boolean, Number).
If you're planning on doing Java processing on the data, or you want to use a persistence framework like Hibernate, you'll need "Full Data Binding". This uses POJOs and annotations to provide a more complex structure to your objects.
There is a good, very concise tutorial covering both options here:
http://wiki.fasterxml.com/JacksonInFiveMinutes

MongoDB - Merging two DBObjects

I am writing a model factory, for which I use JSON to load up a MongoDB DBObject like this:
import com.mongodb.util.JSON;
DBObject dbObject = (DBObject) JSON.parse("{'name':'jack', 'age':30}");
Now, I am trying to break up my JSON files such that I can load up a DBObject with one JSON file, and if needed I can augment the DBObject with another JSON file.
Although it sounds weird, imagine having a set of different type of users. Like, BasicUser, AdvancedUser etc. I can have a JSON file to load up BasicUser, and put the other non-overlapping details of an AdvancedUser in another JSON file. I can make AdvancedUser extend BasicUser, and so I can just combine the contents of the two JSON files to create an AdvancedUser model.
How could I achieve something like this?
I believe putAll is what you want.
DBObject obj1 = (DBObject) JSON.parse("{'name':'jack', 'age':30}");
DBObject obj2 = (DBObject) JSON.parse("{'role':'admin'}");
obj1.putAll(obj2);
System.out.println(obj1.toString()); //{ "name" : "jack" , "age" : 30 , "role" : "admin"}
I decided to roll out my own function to do this by recursively traversing one DBObject and transferring the contents to another.

Categories