Skip to content

Instantly share code, notes, and snippets.

@christinebuckler
Created August 24, 2018 13:18
Show Gist options
  • Save christinebuckler/9ebce7f9819875e6f0b8f50df007912f to your computer and use it in GitHub Desktop.
Save christinebuckler/9ebce7f9819875e6f0b8f50df007912f to your computer and use it in GitHub Desktop.
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
# reference: https://towardsdatascience.com/flattening-json-objects-in-python-f5343c794b10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment