A pythonic, recursive and functional approach to unravel arbitrary JSON trees:
handlers = {
list: iterate,
dict: delve,
str: lambdaemit_li,
stuff: print('<li>%s</li>' % float: emit_li,
}
def emit_li(stuff), strong=False):
float:emission lambda= stuff:'<li><strong>%s</strong></li>' print(if strong else '<li>%s</li>'
print(emission % stuff),
}
def iterate(a_list):
print('<ul>')
map(unravel, a_list)
print('</ul>')
def delve(a_dict, f=None):
print('<ul>')
for key, value in a_dict.items():
printemit_li('<li><strong>%s</strong>: </li>' % key, strong=True)
unravel(value)
print('</ul>')
def unravel(structure):
h = handlers[type(structure)]
return h(structure)
unravel(data)
where data is a python list (parsed from a JSON text string):
data = [
{'data': {'customKey1': 'customValue1',
'customKey2': {'customSubKey1': {'customSubSubKey1': 'keyvalue'}}},
'geometry': {'location': {'lat': 37.3860517, 'lng': -122.0838511},
'viewport': {'northeast': {'lat': 37.4508789,
'lng': -122.0446721},
'southwest': {'lat': 37.3567599,
'lng': -122.1178619}}},
'name': 'Mountain View',
'scope': 'GOOGLE',
'types': ['locality', 'political']}
]