Home > database >  How to parse Json Array not separted by comma in java
How to parse Json Array not separted by comma in java

Time:10-23

I have a db.json file which is from a mongodb export. The json is structured like this:

{
"item1" : "test",
"item2" : "test",
"item3" : "test"
}
{
"item1" : "string",
"item2" : "string",
"item3" : "string"
}

As you can see it doesn't have a comma separator and it's not formatted as a collection of serialized objects.

I'm trying to Deserialize this db.json to a java class using Jackson but obviously it only parses the first pair of brackets because this is not recognized as an array in the standard json format. To be recognized it should be something like that.

[{
"item1" : "test",
"item2" : "test",
"item3" : "test"
},
{
"item1" : "string",
"item2" : "string",
"item3" : "string"
}]

I cannot modify the json manually since it's 15GB of text. This is what i tried

ObjectMapper mapper = new ObjectMapper();
Table[] tablesFromJSON = mapper.readValue(Paths.get("db.json").toFile(), Table[].class);

How should i approach this problem?

CodePudding user response:

Table[] tablesFromJSON = mapper.readValue

Does your machine have ~64GB of RAM installed? Because otherwise, any attempt to pull that stunt vs. a 15GB .json file is going to take a very long time and then keel over with an OutOfMemoryError, obviously.

I therefore highly doubt you actually want what you appear to be asking for.

Instead, you want to stream this data. Take the InputStream, use some JSON library to read one json object off of the stream, process this object in java code, and not by storing it in a gigantic ArrayList or HashMap or what not - in a way that it'll be garbage collectable after, and then read some more bytes out of the stream, just enough for the next object, and so on.

Thus: library sanp

  • Related