Return a list of all variable names in a python nested dict/json document in dot notation

dictionary, json, nested-lists, python

Solution

def dot_notation(obj, prefix=''):
     if isinstance(obj, dict):
         if prefix: prefix += '.'
         for k, v in obj.items():
             for res in dot_notation(v, prefix+str(k)):
                 yield res
     elif isinstance(obj, list):
         for i, v in enumerate(obj):
             for res in dot_notation(v, prefix+'['+str(i)+']'):
                 yield res
     else:
         yield prefix

Example:

>>> list(dot_notation(x))
['a', 'b.c', 'd[0].e', 'd[0].f', 'd[1].e', 'd[1].f']

Problem

I'm looking for a function that operates on a python arbitrarily nested dict/array in JSON-esque format and returns a list of strings keying all the variable names it contains, to infinite depth. So, if the object is... ``` x = { 'a': 'meow', 'b': { 'c': 'asd' }, 'd': [ { "e": "stuff", "f": 1 }, { "e": "more stuff", "f": 2 } ] } ``` `mylist = f(x)` would return... ``` >>> mylist ['a', 'b', 'b.c', 'd[0].e', 'd[0].f', 'd[1].e', 'd[1].f'] ```

Original source