Skip to content

Instantly share code, notes, and snippets.

@jsheedy
Last active March 18, 2016 18:26
Show Gist options
  • Save jsheedy/6269304e9a39247c36aa to your computer and use it in GitHub Desktop.
Save jsheedy/6269304e9a39247c36aa to your computer and use it in GitHub Desktop.
JSON stream extractor
#!/usr/bin/env python
""" parses a JSON document on stdin, and prints to stdout the portion of the document
referenced by the Python getter substring given as the first argument.
This implementation uses eval(), so use with care! No warranty implied.
For example
$ curl 'http://api.duckduckgo.com/?q=ramen&format=json' 2> /dev/null | ~/bin/extract_data.py '["RelatedTopics"][0]["Text"]'
RamenA Japanese noodle soup dish.
"""
import argparse
import json
import logging
import sys
parser = argparse.ArgumentParser()
parser.add_argument('getter', help="Python object access string, e.g. ['foo']['bar'][0]")
args = parser.parse_args()
try:
payload = json.loads(sys.stdin.read())
value = eval('payload' + args.getter)
result = value
print(str(result))
except:
logging.warning("failed to retrieve '{}' from input '{}'".format(args.getter, payload))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment