I've been trying to parse a very complex JSON blob with several levels of nesting. The structure of the JSON returned by the server can change for every call.
I have written a recursive JSON parser using org.json, but the performance isn't great. I do know that JSONReader offers better performance and have been trying to write a module that parses this complex JSON recursively using JSONReader.
From what I see, JSONReader is better for streams where the structure of the JSON is known. Is it advisable to use this for JSON that can change continuously? The example in the Android docs (and other examples i found online) assumes that the JSON has a fixed structure.
Related
I would like to extract only the data present in a Json to JsonForm using Java.
Which framework can I use to this operation?
I've found the answer. Don't let the ugly aspect of JsonForm deceive you. I don't know if this apply for all cases, but I just had to extract the Json object that contains only the data. This can be done using any common Json parser, as like as Gson.
I know a JSON that maintains order is no longer JSON, but this is a requirement in some use and throw code. I have read other hacks like replacing HashMap with LinkedHashMap in the JSONObject.java file. I was wondering if there is a parser which does this without any modifications.
You could use a streaming JSON parser, e.g. Jackson, which will deliver the tags as it finds it. Of course that means that you might have to change your program logic to process the JSON elements on the fly.
What is the easiest to use JSON Java library to parse a JSON (It's structure may vary so I can't mapp it to a Java class as I seen multiple libraries do it) update some elements in an array in this JSON and then save it back to disk?
There are so many libraries for JSON in Java (Gson, Jackson, etc.) and so complex that seem like a total overkill for what i need as opposed to other programming languages.
What's the most straightforward one to use? (that maybe also has a few examples on how to do this)
I have had great success with Json-Simple.
You can check it out here.
I used it to parse 1.5 Million Twitter Streaming data (which is in JSON).
You may find some sample code here on my blog.
You can use java-json jar. The docs for this jar can be found here
I use net.sf.json to do this.
here is an example :
String fromFile="{\"he\",\"hello\"}" //read from file instead
JSONObject object=(JSONObject)JSONSerializer.toJSON( fromFile );
object.put("he", "hello2");
System.out.println(object);
output:
{"he":"hello2"}
In my hadoop class they are wanting us to learn how to write/configure custom input format classes to handle data that isn't simply one record per line (like TextInputFormat).
The data is JSON data from the Twitter API, so it is basically a list of small JSON objects each representing tweets.
[
{
"text":"tweet string",
...more...
},
{
....
},
]
The lab spec suggests focusing on writing a custom recordReader class that manually parses JSON in the given InputSplit, using something like a stack to keep track of opening/closing braces. Frankly this seems idiotic to me, I should not have to reinvent the wheel of parsing JSON... that's the advantage to using a standard data format.
Instead I was hoping to come up with a better solution using an existing JSON parser like json-simple. It seems I should be able to make a custom InputSplit class that splits the list of JSON objects into smaller sublists, and then the custom recordReader would also use the JSON parser to get each JSON object individually.
Is this a proper approach? Should I be customizing an InputSplit? Is there a better way to make a large JSON text consumable by a Hadoop MapReduce job?
I'm wondering what is the fastest way to parse json in JAVA ?
Getting a default object graph using the library built in Array, and Object objects
Getting a custom object graph using your own java bean
Thanks
Well, The newest and wickedly Fastest one is Boon Json. I used it in my project and got an improvement of 20X. I actually got scared and double checked to see if Library is functionally correct. Thankfully, it is :) :)
Boon has built in methods to serialize and de-serialize from/to Java Array/Maps and Custom Beans.
More Here : https://github.com/RichardHightower/boon
Mapping parsed JSON to Java bean involves additional steps, so using the raw interface (e.g. the streaming API of Jackson) will be faster. This way, you can also read until have what you need and stop parsing.
In response to #sikorski
From Jackson Wiki:
Data binding is built using Streaming API as the underlying JSON
reading/writing system: as such it has high-performance [...], but has
some additional overhead compared to pure streaming/incremental
processing
This is pretty much inevitable. If you are writing a generic Jackson parser, you obviously can't use custom types in it. Therefore it follows that you'll have to construct the custom type after you read the JSON with the generic parser, and hence the generic parser will be faster. It's worth noting though that such overhead is very small and almost never something you need to optimize away.
You can use json-simple its a high performance library and its very easy to use :
JSONParser parser=new JSONParser();
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=parser.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();
JSONObject obj2=(JSONObject)array.get(1);
System.out.println("======field \"1\"==========");
System.out.println(obj2.get("1"));
s="{}";
obj=parser.parse(s);
System.out.println(obj);
s="[5,]";
obj=parser.parse(s);
System.out.println(obj);
s="[5,,2]";
obj=parser.parse(s);
System.out.println(obj);