Trying to flatten the aliases key out from Json
{
"name": "Rocky Marci",
"aliases": ["Rocky", "Champ"],
"physical": {
"height_in": 67,
"weight_lb": 150
},
"Fights": 49
}
to like this below
{
"name": "Rocky Marci",
"aliases.0": "Rocky",
"aliases.1": "Champ",
"physical.height_in": 67,
"physical.weight_lb": 150,
"Fights": 49
}
I tried this
def flatten(d, parent_key='', sep='_'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
but this doesn't flatten out the array