Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def flatten_json(nested_json):
- """Flatten json file with nested keys into a single level.
- Args:
- nested_json: A nested json file.
- Returns:
- The flattened json file if successful, None otherwise.
- """
- out = {}
- def flatten(x, name=''):
- if type(x) is dict:
- for a in x:
- flatten(x[a], name + a + '_')
- elif type(x) is list:
- i = 0
- for a in x:
- flatten(a, name + str(i) + '_')
- i += 1
- else:
- out[name[:-1]] = x
- flatten(nested_json)
- return out
Add Comment
Please, Sign In to add comment