Converting JSON Object(s) from file using Java - java

I have a JSON file with no clue on how data will be in it nor the structure of data.
The only thing known is that it will have either an array of JSON objects or a single JSON object.
I need to get each object from the file and store it as a separate item. In case of array of objects in the file, I should get an array of JSON strings which I can store in DB.
Basically, I need to read this file and separate out each JSON object from it and store it in DB as a string.
One of the ways to do it was to use JACKSON ObjectMapper and assign these items to a Hashmap as key value pairs, but I am not sure though how it can be done If there are list of JSON Objects in the file.
Sample JSON File:
[
{
"name":"Bob",
"type":"Email",
"from":"a#a.com",
"to":"b#B.com",
"attachments":[...],
.
.
.
}
]

Do you know the Object structure that the JSON has(let it be Array or a single one) ? If Yes,
First load the json string form the file into an in memory string.
check the string for Array existence, by searching for '[',']' in the outer structure of multiple occurrences of '{' or '}'
once you know whether you have an array or a single object, you can pass it as object reference to either Jackson or GSON parsers
create in memory Array of JsonObject.class say List. It is actually better to enclose this List inside another class. say myJsonObjects and have a List inside it.
Let us see GSON parsers (by google), though Jackson can also be used in the similar implementation
Gson gson = new Gson();
if(isArray){
myJsonObjects jsonArray = gson.fromJson(jsonStringFromFile,myJsonObjects );
}
else{
gson.fromJson(jsonStringFromFile,JsonObject);
}
http://google-gson.googlecode.com/svn-history/trunk/gson/docs/javadocs/com/google/gson/Gson.html

Jackson is my favorite JSON-to-POJO library. It doesn't really matter where you're loading the JSON from (a URL or from the filesystem), there are handlers for several input sources.
Here's an example:
Map<String,Object> userData = mapper.readValue(new File("user.json"), Map.class);
As far as having an unknown number of JSON structures that you're about to parse, the first thing that comes to mind is to have a mapper for each type you're expecting. You could then wrap the parsing code in try/catch blocks so that if the first fails with whatever exception Jackson gives you when encountering an unexpected format, you can then try the next format and so on.
If you're just trying to generically parse JSON that you don't know the structure of beforehand, you can try something like this:
mapper.readValue(jsonString, new TypeReference<List<EntryType>>() {});
The documentation for Jackson is pretty good-- giving it a solid read-through should definitely help. Here's a good five minute tutorial: http://wiki.fasterxml.com/JacksonInFiveMinutes

I prefer use Gson:
Gson gson;
Map<String, Object>parameters=gson.fromJson(myString);
the rest is iterate the map, i hope help you

Related

How to get hits data from elasticsearch in Java

I have elasticsearch response stored as a String value in java,How to process only hits data
For example, you want to retrieve all data as Car type. Your query response is store in searchResponse variable, get all hits and serialize them to the objects.
Look at the example below:
Gson gson = new Gson();
var flowers = new ArrayList<Flower>();
Arrays.stream(searchResponse.getHits().getHits()).forEach(hit ->
cars.add(gson.fromJson(hit.getSourceAsString(), Car.class)));
Of course, I am using gson to serialize the JSON to the object.
The best way to access it from Java is to use the official Java REST API from Elastic. The API will let you work with Java objects instead of processing the data yourself.
You first need to convert the json string to a json/map object, either using gson, Jackson, or other method.
Then, once you have a Map, the hits are under: hits.hits key, as an array of maps, each map in the array represent a hit, with its metadata. The original doc is under the key _source in each hit.
I also highly recommend reading elasticsearch docs, which are good source.

Same JSON Strings when converting to json string cross platform

If I have a json String {"k":"v","a":"b"}.
If I convert it into a json Object and then back to String in Java let say using Gson library and store it in some database.
And Also I convert it into json Object and back to String in Python , it is possible that I get the String as {"a":"b","k":"v"} , though json object will be same but now I cannot do a string match as the order is changed.
How can I solve this problem ?
There is no guarantee that order of json object keys will be same.
Json object is unordered by specefication: http://json.org/
An object is an unordered set of name/value pairs.
If you want some order, you should use json array instead of json object.
An array is an ordered collection of values.
see also: ECMAScript Language Specification
Ensure that the keys are always in sorted order when the JSON is serialized to the database. In Python you would write:
json.dumps(obj, sort_keys=True)
With GSON this is harder to do, you may need to use a different library.

Parsing JSON into a string array or xml

I am trying to connect to a server which than connect to Google Places API and returns me my required data, the data that is being returned is in this format Here
Now in my Android Application i have this as a string or string[], issue is how can i now parse it as an XML or convert it to a native type like a List or something so i can than use it?
If you look at the returned string the actually array results starts after the two elemenets html_attributions & next_page_token so how can i seperate these and parse. Please help.
I would recommend you using the Gson library from Google, it can easily convert JSON -> Object . Here is a nice tutorial for you : tutorial.
This library is used by famous library Retrofit which is made for these calls and it will download the data and convert it to object using Gson : retrofit
There are a variety of ways to perform Json parsing in android.
If you only want to take the json and convert it to a java objects, then you can use gson. When serializing the object, gson will ignore any key in the json that doesn't match the java object that you are going to use for serialization. Therfore, just create your java object with the results instance variable,
https://code.google.com/p/google-gson/
You can use the built in JsonReader class to obtain the "results" and place that into a json array, if you want to single that out.
It is also good to become familiar with retrofit. This library provides a little more functionality than your solution needs however. This library abstracts the whole http request, response, json parsing and gson serializtion for you. So simply, you call a method on the object and get back the json in already serialized pojo.
http://square.github.io/retrofit/
If your returned data is in json format, create one JsonObject, Parse it as Json instead of xml, you can retrieve the value and create String[]. Take help from this tutorial
Of-course you can use Gson library to serialize and de-serialize jsonObject to pojoObject.
Ideally you should use a cool convenient library like Google GSON as mentioned by others for JSON parsing, but I'll explain how to use your json string with the old simple org.json library anyway. If your json string returned from server is in a string s, do:
JSONObject places=new JSONObject(s);
JSONArray results=places.getJSONArray("results");
for(int i=0;i<results.length();i++){
System.out.println(results.getJSONObject(i).getString("place_id"))
System.out.println(results.getJSONObject(i).getString("scope"));
}

Parsing a json object which has many fields

I want to parse json from a server and place it into a class. I use json4s for this. The issue is that a json object contains too many fields, it's about 40-50 of them, some of them have the long names.
I wonder, what will be a sensible way to store all of them, will I have to create 40-50 fields in a class? Remember, some of them will have the long names, as I said earlier.
I use Scala, but a Java's approach might be similar to it, so I added a tag of Java also.
I don't know json4s but in Jersey with Jackson, for example, you can use a Map to hold the Json data or you can use a POJO with all those names.
Sometimes its better to have the names. It makes the code much easier to understand.
Sometimes its better to use a Map. For example, if the field names change from time to time.
If I recall it correctly, using pure Jackson you do something like this:
String jsonString = ....; // This is the string of JSON stuff
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory); // A Jackson class
Map<String,Object> data = mapper.readValue(jsonString, HashMap.class);
You can use a TypeReference to make it a little cleaner as regards the generics. Jackson docs tell more about it. There is also more here: StackOverflow: JSON to Map
There are generally two ways of parsing json to object
1) Parse json to object representation.
the other which might suit you as you mention that your object has too many fields is amap/hashtable, or you could just keep it as JObject, an get fields ehrn you need them

CSV or JSON file from List

So, I have populated a List from an internally stored SQLite table. I would like to send this list to a web service. Can I send the list itself or should I store it in a JSONarray or CSV file? Is there an easy way of converting the List to either of these. Please provide some sample code as I am having trouble coming up with it.
In the interest of following standards, I believe you should definitely use JSON. If its a list, its very easy to construct the JSON with simple string concatenation.
I imagine the JSON would look like
{ col1:"Value1", col2:"Value2", col3:"Value3" }
For JSON, You could try the Gson library for ease of conversion. The Jackson library might be more efficient. Since you asked for an example, the following is a snippet from the Gson page
Collections Examples
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]
(Deserialization)
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
Otherwise have a look at protobufs - probably more efficient.
On android you might have issues with Gson speeds in certain situations but it should work

Categories

Resources