I need to sort out a JSON array into a Hierarchy, here my JSON file, it's never ordered but follow structure:
{
"name":"Folder 2",
"id":"zRDg",
"parent":"OY00",
"type":"folder"
},
{
"name":"Folder 1",
"id":"OY00",
"type":"folder"
},
{
"name":"Folder 3",
"id":"ZDE1",
"type":"folder"
},
{
"name":"DX00025.jpg",
"id":"9Xdd",
"parent":"OY00",
"type":"jpeg"
}
Into this JSON file, the structure is like this:
{
"name":"Folder 1",
"id":"OY00",
"type":"folder",
"children": [{
"name":"Folder 2",
"id":"zRDg",
"type":"folder"
},
{
"name":"DX00025.jpg",
"id":"9Xdd",
"type":"jpeg"
}]
},
{
"name":"Folder 3",
"id":"ZDE1",
"type":"folder"
}
I can't really figure it out, as i'm new to python, my start(wrong):
for index,item in result:
if item['parent']:
for item2 in result:
if item2['id'] == item['parent']:
item['children'] = item2
brake
This is ok, but the problem is it not correct python, folder1/folder2/folder3/ wont work for this, i need a recursive function. I should also include this structure changes, it can be folder withing folder and files withing folder/folders any combination.
myJson = [
{
"name":"Folder 2",
"id":"zRDg",
"parent":"OY00",
"type":"folder"
},
{
"name":"Folder 1",
"id":"OY00",
"type":"folder"
},
{
"name":"Folder 3",
"id":"ZDE1",
"type":"folder"
},
{
"name":"DX00025.jpg",
"id":"9Xdd",
"parent":"OY00",
"type":"jpeg"
}
]
#this creates a dictionary that maps id names to JSON items.
#ex. itemsKeyedById["9Xdd"] gives the jpg item with id "9Xdd"
itemsKeyedById = {i["id"]: i for i in myJson}
#iterate through each item in the `myJson` list.
for item in myJson:
#does the item have a parent?
if "parent" in item:
#get the parent item
parent = itemsKeyedById[item["parent"]]
#if the parent item doesn't have a "children" member,
#we must create one.
if "children" not in parent:
parent["children"] = []
#add the item to its parent's "children" list.
parent["children"].append(item)
#filter out any item that has a parent.
#They don't need to appear at the top level,
#since they will appear underneath another item elsewhere.
topLevelItems = [item for item in myJson if "parent" not in item]
print topLevelItems
Output (with indentation added by me):
[
{
'name': 'Folder 1',
'id': 'OY00',
'type': 'folder',
'children': [
{
'name': 'Folder 2',
'id': 'zRDg',
'parent': 'OY00',
'type': 'folder'
},
{
'name': 'DX00025.jpg',
'id': '9Xdd',
'parent': 'OY00',
'type': 'jpeg'
}
]
},
{
'name': 'Folder 3',
'id': 'ZDE1',
'type': 'folder'
}
]
It also works with items that are nested more than one deep. Example input:
myJson = [
{
"name":"TopLevel folder",
"id":"0",
"type":"folder",
},
{
"name":"MidLevel folder",
"id":"1",
"type":"folder",
"parent":"0"
},
{
"name":"Bottom Level folder",
"id":"2",
"type":"folder",
"parent":"1"
},
{
"name":"Vacation Picture",
"id":"3",
"type":"jpg",
"parent":"2"
},
]
Output:
[
{
'type': 'folder',
'name': 'TopLevel folder',
'id': '0',
'children': [
{
'type': 'folder',
'name': 'MidLevel folder',
'parent': '0',
'id': '1',
'children': [
{
'type': 'folder',
'name': 'Bottom Level folder',
'parent': '1',
'id': '2',
'children': [
{
'type': 'jpg',
'name': 'Vacation Picture',
'parent': '2',
'id': '3'
}
]
}
]
}
]
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With