I have a call to an API that returns me the following response. I am struggling to work with it and access all children since all children can be parents as well.
The response comes in the following hierarchy:
[
{
"id": 459029874,
"name": "Data",
"children": []
},
{
"id":762606928,
"name":"Top 25 KPIs",
"children":[
{
"id":97002306,
"name":"KPI Definition",
"children":[]
},
{
"id":1178185669,
"name":"DEU",
"children":[
{
"id":146196511,
"name":"DEU Development Path",
"children":[]
}
]
}
]
}]
I am trying to get to the following result and then put it in a dataframe:
{"id": 459029874, "name": "Data", "parent_id": ""},
{"id": 762606928, "name": "Top 25 KPIs", "parent_id": ""},
{"id": 97002306, "name": "KPI Definition", "parent_id": "762606928"},
{"id": 1178185669, "name": "DEU", "parent_id": "762606928"},
{"id": 146196511, "name": "DEU Development Path", "parent_id": "1178185669"}
and so forth...
It's convenient with these kind of problems to create a generator that yields the results then yields from the children recursively passing the children back to the generator. It makes for very succinct solutions:
def flatten(l, parent = ''):
for item in l:
yield {'id': item['id'], 'name': item['name'], "parent_id": parent }
yield from flatten(item['children'], item['id'])
list(flatten(data))
Result:
[{'id': 459029874, 'name': 'Data', 'parent_id': ''},
{'id': 762606928, 'name': 'Top 25 KPIs', 'parent_id': ''},
{'id': 97002306, 'name': 'KPI Definition', 'parent_id': 762606928},
{'id': 1178185669, 'name': 'DEU', 'parent_id': 762606928},
{'id': 146196511, 'name': 'DEU Development Path', 'parent_id': 1178185669}]
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