OutOfMemoryError when Jackson deserializes JSON object - java

I have a problem with a very large JSON file that is too large to use ObjectMapper.readValue() into a JsonNode. I would like to use the solution from Out of memory error while parsing a large JSON using Jackson library on Android, except the JSON file is a single object with field names that are not known ahead of time, so I can't create a model POJO to deserialize to.
Each property inside the object has the same format, and I can ignore many of the properties of those inner objects (I already have a POJO class to model that). It would be easier for me to solve this problem if the JSON file was an array instead of an object. (I'm not the one creating the file, just reading from it.)
(I'm posting my solution below, but I hope there's a better one!)

Without being able to load the original file in an ObjectMapper, I decided to parse the JSON file and rewrite it as an array. Reading and writing line-by-line, I converted a file that looks like this (but much larger):
{
"Unexpected Monkey" : {
"name" : "UnexpectedMonkey",
"age" : 7
},
"Another Unexpected Name" : {
"name" : "Another Unexpected Name",
"age" : 2
}
}
into:
[
{
"name" : "UnexpectedMonkey",
"age" : 7
},
{
"name" : "Another Unexpected Name",
"age" : 2
}
]
Then I could parse the file a la How to parse a JSON string to an array using Jackson

Related

How to fetch sub json from request parameter

I just decided to send below json data from client to server. Then i found all my previous request were not of type json. And i am unable to send json. Below is the json i want to send in data of jquery ajax.
data:{
id:"10",
sampleArr:[
{ id:"hello","sample":"hello"},
{ id:"hello1","sample":"hello1"}
]
}
and at server i get below parameters
id=10
group[0][id]=hello
group[0][sample]=hello
group[1][id]=hello1
group[1][sample]=hello1
so i am confused how to fetch all groups
One problem is that what you are sending is not valid JSON.
{ "data" : {
"id" : "10",
"sampleArr": [
{ "id" : "hello", "sample" : "hello"},
{ "id" : "hello1", "sample" : "hello1"}
]
}
}
Notice that all attribute names must be quoted, and the top-level JSON object must have curly brackets around it.
If that doesn't help, you need to explain how your servlet is receiving and parsing the JSON.
#BigMike, Thanks i am able to fetch complete json and play around with it. Was unable to send JSON even setting content type application/json. But Still checking why, but working as temperary fix (Might be that i am not using Rest API)

Is there a better way to create a Spring DTO to get a file or a folder containing files than encoding in base64 string?

At the moment, the file's content is transferred in a JSON like this :
{
"name" : "filename.txt",
"path" : "src/info/filename.txt",
"base64content" : "dGhpcyBpcyBhIGZpbGUgd2l0aCBjb250ZW50"
}
I just want to know how you would structure the JSON (and possibly the DTO class object) ?

Is there a better/easier way to update a json file in Java?

Say, I have a json file like below:
[{
"obj1_key1":"aa",
"obj1_array":[{"e1":"11"},{"e2":"22"}]
},
{
"obj2_key1":"cc",
"obj2_key2":"dd"
}]
Now I want update the file into something like below:
[{
"obj1_key1":"aa",
"obj1_array":[{"e1":"11"},{"e2":"22"},{"e3":"333"}]
},
{
"obj2_key1":"cc",
"obj2_key2":"dd"
}]
I tried using ObjectMapper to parse the file like
JsonNode jsonFile = new ObjectMapper().readTree(new File("file.json");
however then I need to find the obj1_array and append a json object, then write the json object back to the file. And I don't think the way I load the json file as a JsonNode is a easy way because I should convert it between Json/JsonArray back and forth. So I'm wondering is there a simpler way to make this work? Really appreciate that.
if it is just a one-off case, you can use your preferred mechanism, but if it is going to used often, I would prefer
Convert the JSON to a POJO ( using some parser eg Jackson )
Update the requisite fields
Return the Json object.

Getting the response from a json response to Google Maps API in Java

I need to develop a program where I have to calculate driving distance and time. Getting the request to the Google Maps API works, and I get a really big response. Now I want to extract the data I need from this response. The response is in json, and the program I make is made in Java. I have no idea to do this. I tried to just put the whole response in a string, and then search the data I need, like "distance" and extract a few characters that come after that. Problem with this is, if something changes in front of it, it's all going to be at different indexes in the huge string.
A response is like this (only a bit of the big thing)
"copyrights" : "Kaartgegevens ©2017 GeoBasis-DE/BKG (©2009), Google",
"legs" : [
{
"distance" : {
"text" : "229 km",
"value" : 229411
},
"duration" : {
"text" : "2 uur 20 min.",
"value" : 8417
},
How do I easily extract the value of both "distance" and "duration"?
Basically what you want to do is serializing and deserializing the JSON to a proper Java object. You can do this by using Frameworks like Jackson or GSON to convert the JSON-String to POJOs.
This might help you as well:
How to serialize and deserialize a JSON object from Google geocode using Java
Converting JSON to Java
http://thegeekyland.blogspot.de/2015/11/serializing-and-deserializing-json-from.html
And I'm pretty sure Google has it's own API that maps the JSON internally, although I haven't used it yet. Here you might find more information:
https://developers.google.com/maps/web-services/?hl=de
Hope this helps!

Accessing JSON array elements in java

I have a json file and have used simple.json jar to parse the elements. I could parse the elements successfully. But what I want is that if my json file has three elements by same name, then I want to print each name only when their index is called.
get() prints out all the elements of that name.
Please help!
Following is the json file:
{
"nodes":
[
{
"node":"1",
"ipaddr":"127.0.0.1",
"port":"8443",
"mgport":"9000"
},
{
"node":"2",
"ipaddr":"127.0.0.1",
"port":"8556",
"mgport":"9000"
},
{
"node":"3",
"ipaddr":"127.0.0.1",
"port":"8000",
"mgport":"9000"
}
]
}
I need to retrive only one port value rather than all the values.
I have successfully used the JSON Processing library to do something similar to what you describe.
Here is the link for you to have a look: https://jsonp.java.net/
Could you post also an example of the JSON you'll like to parse?
Use Jackson for parsing JSON. It will make your life easier.
This will help you.
http://www.tutorialspoint.com/jackson/jackson_first_application.htm
Thanks

Categories