How to read a JSON file containing multiple root elements?

tshepang picture tshepang · Jul 24, 2012 · Viewed 21.8k times · Source

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
}

Answer

userSteve picture userSteve · May 9, 2018

No one has mentioned arrays:

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

Is valid JSON and might do what the OP wants.