0

I need to parse large file with more than one JSON in it. I didn't find any way how to do it. File looks like a BSON for mongoDB. File example:

{"column" : value, "column_2" : value}
{"column" : valeu, "column_2" : value}
....
Ihar Sadounikau
  • 741
  • 6
  • 20
  • possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – kevcodez Jul 09 '15 at 10:08
  • Thanks, but It didnt. All example parse only one json in file... – Ihar Sadounikau Jul 09 '15 at 10:19
  • "parse each line to a Json object using json-simple" from top answer at http://stackoverflow.com/questions/25346512/read-multiple-objects-json-with-java which includes an example –  Jul 09 '15 at 11:34

1 Answers1

0

You will need to determine where one JSON begins and another ends within the file. If each JSON is on an individual line, then this is easy, if not: You can loop through looking for the opening and closing braces, locating the points between each JSON.

    char[] characters;
    int openBraceCount = 0;
    ArrayList<Integer> breakPoints = new ArrayList<>();

    for(int i = 0; i < characters.length; i++) {
        if(characters[i] == '{') {
            openBraceCount++;
        } else if(characters[i] == '}') {
            openBraceCount--;

            if(openBraceCount == 0) {
                breakPoints.add(i + 1);
            }
        }
    }

You can then break the file apart at each break point, and pass the individual JSON's into whatever your favorite JSON library is.

Pherion
  • 165
  • 10