Skip to content

Instantly share code, notes, and snippets.

@colehocking
Created July 11, 2018 15:51
Show Gist options
  • Save colehocking/f8e07e20c0b802288b568a9cb5824de0 to your computer and use it in GitHub Desktop.
Save colehocking/f8e07e20c0b802288b568a9cb5824de0 to your computer and use it in GitHub Desktop.
dumpClean: the mighty nested JSON object expander!
def dumpClean(obj):
"""
Recursive function to clean the output of deeply nested dicts/lists
"""
if type(obj) == dict:
for k, v in obj.items():
# if the object has an iterable attribute; keep pulling it apart
if hasattr(v, '__iter__'):
print("%s : %s" % (k, dumpClean(v)))
else:
print('%s : %s' % (k, v))
elif type(obj) == list:
for o in obj:
if hasattr(o, '__iter__'):
dumpClean(o)
else:
print("%s" % (str(o)))
else:
print(str(obj))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment