Skip to content

Instantly share code, notes, and snippets.

@igneus
Created March 27, 2015 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igneus/4732b8e232660e55832d to your computer and use it in GitHub Desktop.
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 ...
#!/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