How to save the JSON data to the database - java

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

Related

Mongodb java driver get result as string not bson object

Is there a way to do a query where only the string is returned and not converted to bson object?
I want to use jsoniter to do the deserialization, but don't know if this can be done using java driver.
You can take the bson object and call toJson() on it to get a String that contains the query result in json format. That should allow you to use Jsoniter or Gson to deserialize your query's result.
Example:
Document result = collection.find(eq("birthYear", 1990)).first(); // Made up query
String resultJsonString = result.toJson();
You can read more about the toJson() method in the MongoDB documentation for the Java DB Driver.

Map JSONObject to SQL server

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.

MongoDB Java Driver

Given some JSON value and a query in MongoDB format, I want to filter the same way that MongoDB does, the json entities I want without going to the MongoDB.
For example, I have:
JSON Value: [{qty: 10}, {qty: 30}, {qty: 50}]
Query in MongoDB format: { qty: { $gt: 20 } }
Result: [{qty: 50}]
I want that without going to Mongo database, for example calling some method that recives JSON Value and JSON Query String in Mongo format, inside some JAR.
Thanks!
I want that without going to Mongo database
Parse JSON using Jackson and create a Query Object and a Collection containing the target objects.
Use a collections framework such as Guava or GS-Collections and filter.
'Jackson' library offers JSON parsing & generation in Java. Once you've parsed, you can filter values/ data structure using Java code to your heart's content.
Java obviously has no direct implementation of Mongo query language.. you can implement Java code yourself as desired.
See:
http://jackson.codehaus.org/

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!

JSON data store in POSTGRES (EclipseLink)

I would like to deisgn a database and model like below for eclipseLink and POSTGRES.
What would be the best approach to do the same? How ro store JSON data in Postgres and retrieve?
My data model is something like below.
NAMES SURNAMES
id name id JSON
1 test 100 {test:temp, test2:dfdf, test3:fsdf}
Mapping
namesId surnamesId place country
1 100 San Jose CA
You can store JSON in any character column (VARCHAR, TEXT). If you want to use the JSON type you may need to use a Converter to convert between it and your field type.
If you want to convert your JSON data to objects, you can use JAXB with EclipseLink Moxy to do this.
http://wiki.eclipse.org/EclipseLink/Examples/MOXy#MOXy.27s_JSON-Binding
You could write your own converter for this, or in the EclipseLink 2.6 dev stream there is a new JSON Convert option.
See,
http://wiki.eclipse.org/EclipseLink/DesignDocs/406993

Categories