0

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

Chamath
  • 2,016
  • 2
  • 21
  • 30
arunve
  • 3
  • 4

2 Answers2

1

You can use recursion:

s = {
 "name": "Rocky Marci",
 "aliases": ["Rocky", "Champ"],
 "physical": {
    "height_in": 67,
    "weight_lb": 150
  },
  "Fights": 49
}
def flatten(d, last = ''):
   for a, b in d.items():
     if type(b) in [int, str]:
       yield ("{}.{}".format(a, last) if last else a, b)
     if isinstance(b, list):
       for i, c in enumerate(b):
         yield ("{}.{}".format(a, i), c)
     if isinstance(b, dict):
        for i in flatten(b, last = a):
           yield i

result = dict(list(flatten(s)))

Output:

{'aliases.0': 'Rocky', 'height_in.physical': 67, 'name': 'Rocky Marci', 'aliases.1': 'Champ', 'Fights': 49, 'weight_lb.physical': 150}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

Because list is not an instance of collections.MutableMapping. I suggest checking

isinstance(v, (list, dict))

instead.

More possibilities are here

AleksMat
  • 854
  • 6
  • 11