Last active
July 16, 2018 03:05
-
-
Save stephenquan/93c488eaa39a003f581d7d7969b9c795 to your computer and use it in GitHub Desktop.
flatjson.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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