def flatten(structure, key='', path='', flattened=None):
    if flattened is None:
        flattened = {}
if type(structure) not in (dict, list):
    flattened[(path + '_' if path else '') + key] = structure
elif isinstance(structure, list):
    for i, item in enumerate(structure):
        flatten(item, '%d' % i, path + '_' + key, flattened)
else:
    for new_key, value in list(structure.items()):
        pass
Flattening a list of dicts of lists of dicts (etc) of unknown depth in Python (nightmarish JSON structure)