# HG changeset patch # User Jeff Hammel # Date 1401733306 25200 # Node ID ad8146c60c4da6451855fda56520a91327b6cbee # Parent e50a95358db3be648ad7192c7b107d6d6d2f6566# Parent 34af6d5ac73c16924584b52de713c4085c461d92 merge diff -r 34af6d5ac73c -r ad8146c60c4d python/jsonex.py --- a/python/jsonex.py Sat May 24 15:27:16 2014 -0700 +++ b/python/jsonex.py Mon Jun 02 11:21:46 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