# HG changeset patch # User Jeff Hammel # Date 1401738051 25200 # Node ID d586ea538d36d74a28fb350aab7f8fdd1708b41d # Parent 5e8d3f68a997a4705298463c896f2c85dd6d2d63# Parent 6f48a3e5ab41907841714dfa345bb8546765e627 merge diff -r 5e8d3f68a997 -r d586ea538d36 python/jsonex.py --- a/python/jsonex.py Mon Jun 02 12:38:40 2014 -0700 +++ b/python/jsonex.py Mon Jun 02 12:40:51 2014 -0700 @@ -4,19 +4,37 @@ 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: - foo = arg.split('.') # split into objects - # TODO: split into slice notation - pass # TODO + # 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: + base = obj + for part in o.strip().split('.'): # split into objects + raise NotImplementedError('TODO') else: print json.dumps(obj, indent=2, sort_keys=True)