I have written a function designed to flatten a json. The function works recursively - if a key, value pair's value is not a json, the key,value pair is returned, otherwise, the function is called again on the value:
def flatten_json(j):
for k, v in j.items():
if isinstance(v, dict):
flatten_json(v)
else:
yield (k,v)
flat_json = {x[0]: x[1] for x in flatten_json(j)}
The idea being that whenever the function yields a tuple, it is collected into the flat_json dict. As it stands, nested outputs are ignored - only the top level key, value pairs appear in flat_json.
{'foo': 'bar, 'foo1': 'bar1', 'nest1': {'foo2': 'bar2'}}should return{'foo': 'bar, 'foo1': 'bar1', 'foo2': 'bar2'}