11

If I had a file whose contents looked like:

{"one": 1}
{"two": 2}

I could simply parse each separate line as a separate JSON object (using JsonCpp). But what if the structure of the file was less convenient like this:

{
   "one":1
}

{
   "two":2
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Related question: [How to detect trailing garbage using Jackson ObjectMapper](http://stackoverflow.com/questions/26003171/how-to-detect-trailing-garbage-using-jackson-objectmapper) – sleske Apr 01 '16 at 13:06

5 Answers5

17

No one has mentioned arrays:

[
  {"one": 1},
  {"two": 2}
]

Is valid JSON and might do what the OP wants.

userSteve
  • 1,554
  • 1
  • 22
  • 34
  • That actually did the thing for me. A really good workaround of big files with multiple root elements, usually dumped from some system. – Martin Dimitrov Feb 17 '20 at 12:53
7

Neither example in your question is a valid JSON object; a JSON object may only have one root. You have to split the file into two objects, then parse them.

You can use http://jsonlint.com to see if a given string is valid JSON or not.

So I recommend either changing what ever is dumping multiple JSON objects into a single file to do it in separate files, or to put each object as a value in one JSON root object.

If you don't have control over whatever is creating these, then you're stuck parsing the file yourself to pick out the different root objects.

Here's a valid way of encoding those data in a JSON object:

{
    "one": 1,
    "two": 2
}

If your really need separate objects, you can do it like this:

{
    "one":
    {
        "number": 1
    },
    "two":
    {
        "number": 2
    }
}
Max Play
  • 3,717
  • 1
  • 22
  • 39
Almo
  • 15,538
  • 13
  • 67
  • 95
  • 1
    The first example isn't a valid object either, that's the point of the question. – Jonathan Wakely Jul 24 '12 at 22:00
  • 2
    Sorry, I should have mentioned that we should assume I have no control of the format of the file. – tshepang Jul 24 '12 at 22:05
  • 2
    I am not aware of a JSON parser that will deal with multiple root objects (maybe someone can correct me), so you're probably stuck parsing to count matching {} to find where the root objects are. – Almo Jul 24 '12 at 22:08
  • I found it surprising when struggling to search for such on Google without success. – tshepang Jul 24 '12 at 22:12
  • 8
    I wouldn't expect any library to handle multiple root objects, but I could imagine a library that treats its input as a stream and reads one JSON object off the stream. Calling it a second time would extract the next object, and so on. I don't know whether any JSON library behaves that way, but it seems like a reasonable feature for a hypothetical library to offer. – Rob Kennedy Jul 24 '12 at 22:26
  • @RobKennedy: At least Jackson ( http://wiki.fasterxml.com/JacksonHome ) will actually just ignore any trailing data after the first JSON object, so it does half of what you propose. Dunno if there's a way to "pick up" after the first parse, though. – sleske Apr 01 '16 at 13:08
  • I cannot see the rationale in what is being said here. A sequence of JSON Values (objects or arrays) makes perfect sense, e.g. in some log file or a communications stream. There is no inherent disambiguity in the grammar which would prevent such a use case, nor is there a divine law of json data packaging. So it is rather a tool chain limitation than a problem of the use case. – BitTickler May 11 '21 at 11:59
2

Rob Kennedy is right. Calling it a second time would extract the next object, and so on.Most of the json lib can not help you to do all in a single root. Unless you are using more high end framework in QT.

Ong Ming Soon
  • 1,041
  • 1
  • 10
  • 22
0

You can programmatically add a pair of square brackets around the JSON you receive, and parse bypass the "dummy root" node you just added.

But you don't have to do so manually. There is one such thing called JSON Line that you might be interested in.

Fifnmar
  • 372
  • 1
  • 5
  • 9
-1

You can also use this custom function to parse multiple root elements even if you have complex objects.

    static getParsedJson(jsonString) {
      const parsedJsonArr = [];
      let tempStr = '';
      let isObjStartFound = false;
      for (let i = 0; i < jsonString.length; i += 1) {
          if (isObjStartFound) {
              tempStr += jsonString[i];
              if (jsonString[i] === '}') {
                  try {
                      const obj = JSON.parse(tempStr);
                      parsedJsonArr.push(obj);
                      tempStr = '';
                      isObjStartFound = false;
                  } catch (err) {
                      // console.log("not a valid JSON object");
                  }
              }
          }
          if (!isObjStartFound && jsonString[i] === '{') {
              tempStr += jsonString[i];
              isObjStartFound = true;
          }
       }
       return parsedJsonArr;
   }
AJAY
  • 1
  • 4