Created
January 11, 2011 09:50
-
-
Save kkaefer/774249 to your computer and use it in GitHub Desktop.
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
json() { | |
python - $@ <<"EOS" | |
import json, sys | |
try: | |
obj = json.load(open(sys.argv[1])) | |
for key in sys.argv[2:]: obj = obj[key if type(obj) == dict else int(key)] | |
except ValueError as msg: sys.stderr.write('Invalid JSON: %s\n' % msg); exit(1) | |
except KeyError as msg: sys.stderr.write('Key "%s" does not exist\n' % key); exit(2) | |
if type(obj) == dict: print '\n'.join(obj.keys()) | |
elif type(obj) == list: print '\n'.join(obj) | |
else: print obj | |
EOS | |
return $? | |
} | |
JSON="json package.json" | |
for key in $($JSON engines); do | |
echo $key: $($JSON engines $key) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment