Reputation: 439
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 yield
s 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.
Upvotes: 2
Views: 994
Reputation: 13393
This is exactly what yield from
is for :), to yield elements one by one from calling a generator from within a generator (whether that is a recursive call or not).
try:
def flatten_json(j):
for k, v in j.items():
if isinstance(v, dict):
yield from flatten_json(v)
else:
yield (k,v)
j = {'foo': 'bar', 'foo1': 'bar1', 'nest1': {'foo2': 'bar2'}}
flat_json = {x[0]: x[1] for x in flatten_json(j)}
print(flat_json)
Output:
{'foo': 'bar', 'foo1': 'bar1', 'foo2': 'bar2'}
running example here: https://ideone.com/Z5aO9V
Upvotes: 5