How to Read a JSON File Containing Multiple Root Elements

How to read a JSON file containing multiple root elements?

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
}
}

How to iterate through a JSON file which has multiple root elements using Gson library in Java?

Use a JsonStreamParser to handle multiple top-level elements in the file.

JsonStreamParser parser = new JsonStreamParser(new FileReader("sample.json"));

while (parser.hasNext()) {
JsonElement object = parser.next();
System.out.println(object.getAsJsonObject().get("reviewText"));
}

JSON File multiple roots

# Should look like this [{"ticket": {"id": "123", "name": "bill"}}, {"ticket": {"id": "1234", "name": "james"}}]

import json

with open('ticketData8242020-6152021.json', 'r') as f:
data = f.read()

data = json.loads(data)

How to load json store with multiple root elements?

If you're using 4.x, the root parameter can be a function:

Ext.define('MyModel', {
extend: 'Ext.data.Model',
fields: ['name']
});

Ext.require('*');

Ext.onReady(function(){

var store = new Ext.data.Store({
model: MyModel,
proxy: {
type: 'memory',
reader: {
type: 'json',
root: function(o){
var out = [];
return out.concat(o.root1, o.root2, o.root3);
}
}
},
data: {
root1: [{
name: 'Item 1'
}],
root2: [{
name: 'Item 2'
}],
root3: [{
name: 'Item 3'
}]
}
});

store.load();
console.log(store.getCount());
});

How to read multiline json with root element in Spark Scala?

Try that:

import org.apache.spark.sql.functions._
ds.select(explode($"Crimes") as "exploded").select("exploded.*")

where ds is your Dataset<Row> you created from the JSON record.

Please note that if your data is huge Spark will need to hold the entire data in memory before flattening it.

get JSON root elements containing inner elements

Something like this? Might not be the optimal solution.

#Load JSON
json_str = '[{"firstRoot":{"firstInner1":"test","secondInner1":"test"}},{"secondRoot":{"firstInner2":"test","secondInner2":"test"}},{"thirdRoot":{"firstInner3":"test","secondInner3":"test"}}]'
dic = json.loads(json_str)

#Output arrays
root_ele = []
inner_ele = []

#Parse JSON
for i in dic:
root_ele.append(list(i.keys())[0])
y = [k for j in list(i.values()) for k, l in j.items()]
inner_ele.append(y)

#Print output
print(root_ele)
print(inner_ele)

Output:

['firstRoot', 'secondRoot', 'thirdRoot']
[['firstInner1', 'secondInner1'], ['firstInner2', 'secondInner2'], ['firstInner3', 'secondInner3']]


Related Topics



Leave a reply



Submit