# HG changeset patch # User Jeff Hammel # Date 1401733219 25200 # Node ID e50a95358db3be648ad7192c7b107d6d6d2f6566 # Parent 3da85ac3c54f09218ff72908100377a77d4da064 allow loading urls diff -r 3da85ac3c54f -r e50a95358db3 python/jsonex.py --- a/python/jsonex.py Mon May 19 13:16:53 2014 -0700 +++ b/python/jsonex.py Mon Jun 02 11:20:19 2014 -0700 @@ -4,16 +4,34 @@ JSON explorer """ +import argparse import json +import os import sys -from pprint import pprint +import urllib2 def main(args=sys.argv[1:]): - data = sys.stdin.read() # read from stdin - obj = json.loads(data) + + # command line + parser = argparse.ArgumentParser(description='__doc__') + parser.add_argument('input', nargs='?', + help="input file or url (read from stdin if ommitted)") + parser.add_argument('object', nargs='*', + help="object in dotted notation") + + options = parser.parse_args(args) - if args: - for arg in args: + # get data + if not options.input or options.input == '-': + data = sys.stdin + elif'://' in options.input: + data = urllib2.urlopen(options.input) + else: + data = open(options.input, 'r') + obj = json.load(data) + + if options.object: + for o in options.object: foo = arg.split('.') # split into objects # TODO: split into slice notation pass # TODO