python - Sorting JSON object(s) into a Hierarchy -


i need sort out json array hierarchy, here json file, it's never ordered 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 json file, structure 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 figure out, i'm new python, start(wrong):

for index,item in result:     if item['parent']:         item2 in result:             if item2['id'] == item['parent']:                 item['children'] = item2                 brake  

this ok, problem not correct python, folder1/folder2/folder3/ wont work this, need recursive function. should include structure changes, can folder withing folder , files withing folder/folders 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 dictionary maps id names json items. #ex. itemskeyedbyid["9xdd"] gives jpg item id "9xdd" itemskeyedbyid = {i["id"]: in myjson}  #iterate through each item in `myjson` list. item in myjson:     #does item have parent?     if "parent" in item:         #get parent item         parent = itemskeyedbyid[item["parent"]]         #if parent item doesn't have "children" member,          #we must create one.         if "children" not in parent:             parent["children"] = []         #add item parent's "children" list.         parent["children"].append(item)  #filter out item has parent. #they don't need appear @ top level,  #since appear underneath item elsewhere. toplevelitems = [item item in myjson if "parent" not in item] print toplevelitems 

output (with indentation added 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 works items nested more 1 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'                             }                         ]                      }                 ]             }         ]     } ] 

Comments