Skip to content

Instantly share code, notes, and snippets.

@ravila4
Created September 12, 2019 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravila4/42e59426d8de36ab853d988bfc6d6b67 to your computer and use it in GitHub Desktop.
Save ravila4/42e59426d8de36ab853d988bfc6d6b67 to your computer and use it in GitHub Desktop.
Recursive function for flattening JSON.
def flatten_json(y):
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(y)
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment