Created
March 27, 2015 15:13
-
-
Save igneus/4732b8e232660e55832d to your computer and use it in GitHub Desktop.
if you have a lot of json files with the same structure and need some piece from all of them ...
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/python | |
# jsongetter.py | |
# | |
# reads json file/s, | |
# gets a part specified in a Python dict/list notation, | |
# prints formatted contents | |
# | |
# $ jsongetter.py <keys> [file1 file2 ...] | |
# | |
# $ jsonformatter "['uncle']['name']" uncles/joe.json | |
# Joe | |
import sys, json, traceback | |
query = sys.argv[1] | |
files = sys.argv[2:] | |
for f in files: | |
title = '' | |
if len(files) > 1: | |
title = '== %s\n\n' % f | |
try: | |
data = json.load(open(f)) | |
result = eval('data'+query.strip()) | |
print title + json.dumps(result, indent=2) | |
print '\n' | |
except: | |
pass | |
#print traceback.format_exc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment