Copy keys and list contents from JSON in python
<p>I am trying to skim through a dictionary that contains asymmetrical data and make a list of unique headings.  Aside from the normal key:value items, the data within the dictionary also includes other dictionaries, lists, lists of dictionaries, NoneTypes, and so on at various levels throughout.  I would like to be able to keep the hierarchy of keys/indexes if possible.  This will be used to assess the scope of the data and it's availability.  The data comes from a JSON file and it's contents are subject to change.</p>

<p>My latest attempt is to do this through a series of type checks within a  function, <code>skim()</code>, as seen below.</p>

<pre><code>def skim(obj, header='', level=0):
    if obj is None:
        return

    def skim_iterable(iterable):
        lvl = level +1
        if isinstance(iterable, (list, tuple)):
            for value in iterable:
                h = ':'.join([header, iterable.index(value)])
                return skim(value, header=h, level=lvl)
        elif isinstance(iterable, dict):
            for key, value in iterable.items():
                h = ':'.join([header, key])
                return skim(value, header=h, level=lvl)


    if isinstance(obj, (int, float, str, bool)):
        return ':'.join([header, obj, level])
    elif isinstance(obj, (list, dict, tuple)):
        return skim_iterable(obj)
</code></pre>

<p>The intent is to make a recursive call to <code>skim()</code> until the key or list index position at the deepest level is passed and then returned.  <code>skim</code> has a inner function that handles iterable objects which carries the level along with the key value or list index position forward through each nestled iterable object. </p>

<p>An example below</p>

<pre><code>test = {"level_0Item_1": {
    "level_1Item_1": {
     "level_2Item_1": "value",
     "level_2Item_2": "value"
    },
    "level_1Item_2": {
     "level_2Item_1": "value",
     "level_2Item_2": {}
    }},
    "level_0Item_2": [
     {
      "level_1Item_1": "value",
      "level_1Item_2": 569028742
     }
    ],
    "level_0Item_3": []
   }

collection = [skim(test)]
</code></pre>

<p>Right now I'm getting a return of <code>[None]</code> on the above code and would like some help troubleshooting or guidance on how best to approach this.  What I was expecting is something like this:</p>

<pre><code>['level_0Item_1:level_1Item_1:level_2Item_1',
 'level_0Item_1:level_1Item_1:level_2Item_2',
 'level_0Item_1:level_1Item_2:level_2Item_1',
 'level_0Item_1:level_1Item_2:level_2Item_2',
 'level_0Item_2:level_1Item_1',
 'level_0Item_2:level_1Item_2',
 'level_0Item_3]
</code></pre>

<p>Among other resources, I recently came across this question <a href="http://stackoverflow.com/questions/29283267/python-json-complex-objects-accounting-for-subclassing">(python JSON complex objects (accounting for subclassing)</a>), read it and it's included references.  Full disclosure here, I've only began coding recently.</p>

<p>Thank you for your help.</p>

<p>You can try something like:</p>

<pre><code> def skim(obj, connector=':', level=0, builded_str= ''):
    if isinstance(obj, dict):
        for k, v in obj.items():
            if isinstance(v, dict) and v:
                yield from skim(v, connector, level + 1, builded_str + k + connector)
            elif isinstance(v, list) and v:
                yield from skim(v[0], connector, level + 1, builded_str + k + connector)
            else:
                yield builded_str + k
    else:
        yield builded_str
</code></pre>

<p>Test:</p>

<pre><code>test = {"level_0Item_1": {
    "level_1Item_1": {
     "level_2Item_1": "value",
     "level_2Item_2": "value"
    },
    "level_1Item_2": {
     "level_2Item_1": "value",
     "level_2Item_2": {}
    }},
    "level_0Item_2": [
     {
      "level_1Item_1": "value",
      "level_1Item_2": 569028742
     }
    ],
    "level_0Item_3": []
   }
lst = list(skim(test))
print(lst)
['level_0Item_1:level_1Item_2:level_2Item_1`', 
'level_0Item_1:level_1Item_2:level_2Item_2', 
'level_0Item_1:level_1Item_1:level_2Item_1', 
'level_0Item_1:level_1Item_1:level_2Item_2', 
'level_0Item_2:level_1Item_2',
'level_0Item_2:level_1Item_1', 
'level_0Item_3']`
</code></pre>

python json types nested iterable