Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a JSON file containing multiple root elements?

Tags:

c++

jsoncpp

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
}
like image 541
tshepang Avatar asked Jul 24 '12 21:07

tshepang


People also ask

Can JSON have multiple root elements?

Virtual root element:If the incoming JSON document has multiple root elements, enter a virtual root element to be added to the output XML document. This is required because multiple root elements are not valid in XML. Otherwise, the XML parser will fail.

Can a JSON file have multiple objects?

Can JSON have multiple objects? You can pass a single JSON object to create a single element, or a JSON array of group JSON objects to create multiple elements.

Can a JSON file have multiple arrays?

JSON array can store multiple values. It can store string, number, boolean or object in JSON array. In JSON array, values must be separated by comma.


1 Answers

No one has mentioned arrays:

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

Is valid JSON and might do what the OP wants.

like image 91
userSteve Avatar answered Sep 20 '22 22:09

userSteve