Last active
December 29, 2015 15:09
-
-
Save hyperair/7688394 to your computer and use it in GitHub Desktop.
grson -- greppable JSON dumper
This file contains hidden or 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
#!/usr/bin/python3 | |
import json | |
import sys | |
fmt = '{key} = {value}' | |
def dump_obj(prefix, obj): | |
if isinstance(obj, dict): | |
for k, v in obj.items(): | |
if prefix: | |
k = prefix + '.' + k | |
dump_obj(k, v) | |
elif isinstance(obj, list): | |
for i, v in enumerate(obj): | |
if prefix: | |
k = "{prefix}[{idx}]".format(prefix=prefix, idx=i) | |
dump_obj(k, v) | |
else: | |
print(fmt.format(key=prefix, value=json.dumps(obj))) | |
obj = json.loads(sys.stdin.read()) | |
dump_obj('this', obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment