0

I have multiple JSON objects stored in one file separated by new line character (but one object can span over multiple lines) - it's an output from MongoDB shell.

What is the easiest way to parse them (get them in an array or collection) using Gson and Java?

1
  • There are parsers that can handle this, by leaving the input stream positioned at the end of the prior object/array when there may be more than one. But there are 20-30 different JSON kits for Java, so I can't say which have this feature. Commented Jan 9, 2015 at 18:55

1 Answer 1

1

Another possibility is to use Jackson and its ObjectReader.readValues() methods:

public <T> Iterator<T> readStream(final InputStream _in) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    // configure object mappings
    ...
    // and then
    return mapper.reader(MapObject.class).readValues(_in);
}

works pretty good on big enough (few gigabytes) JSON datafiles

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.