Skip to content

Instantly share code, notes, and snippets.

@stephenquan
Last active July 16, 2018 03:05
Show Gist options
  • Save stephenquan/93c488eaa39a003f581d7d7969b9c795 to your computer and use it in GitHub Desktop.
Save stephenquan/93c488eaa39a003f581d7d7969b9c795 to your computer and use it in GitHub Desktop.
flatjson.py
import json, sys
def combine(attr, prefix):
return attr if prefix == "" else prefix + "." + attr
def flatjson(obj, prefix = ""):
if isinstance(obj, list):
print combine("len", prefix) + "\t" + str(len(obj))
for i, row in enumerate(obj):
flatjson(obj[i], prefix + "[" + str(i) + "]")
elif isinstance(obj, dict):
for attr, val in obj.items():
flatjson(val, combine(attr, prefix))
else:
print prefix + "\t" + json.dumps(obj)
if __name__ == '__main__':
flatjson(json.load(sys.stdin))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment