I have a pretty big JSON file that has a folder, class and object structure that I need to read and export the output to a dataframe. When I try my logic, I am able to get to the first elements but if it has multiple elements, I am unable to see them.
I tried going through multiple posts but had no luck so far. Could anyone guide me on where I am going wrong or if there is a better way to do this ?
My sample JSON file:
{
"root": {
"id": 123456,
"outline": {
"folder": [
{
"name": "First Folder",
"folder": [
{
"name": "First Class",
"item": [
{
"@dataType": "String",
"name": "ID1"
}
]
},
{
"name": "Second Class",
"item": [
{
"@dataType": "Numeric",
"name": "FACTOR1"
}
]
}
]
},
{
"name": "Second Folder",
"folder": [
{
"name": "First Class",
"item": [
{
"@dataType": "String",
"name": "ID2"
}
]
},
{
"name": "Second Class",
"item": [
{
"@dataType": "Numeric",
"name": "FACTOR2"
}
]
}
]
}
]
}
}
}
Expected output to a dataframe:
['First Folder', 'First Class', 'ID1']
['First Folder', 'Second Class', 'FACTOR1']
['Second Folder', 'First Class', 'ID2']
['Second Folder', 'Second Class', 'FACTOR2']
My python code so far:
import json
with open('Sample_File.json') as f_in:
data = json.load(f_in)
for x in data['root']['outline']['folder']:
print('Folder Name : {}'.format(x['name']))
counter = 0
for y in x['folder'][counter]['item']:
print('Class Name : {}, Object Name : {}'.format(x['folder'][counter]['name'], y['name']))
counter = counter + 1
My current output which is not right:
['First Folder', 'First Class', 'ID1']
['Second Folder', 'First Class', 'ID2']