comparison python/jsonex.py @ 691:d586ea538d36

merge
author Jeff Hammel <k0scist@gmail.com>
date Mon, 02 Jun 2014 12:40:51 -0700
parents 6f48a3e5ab41
children 80c47cc35f37
comparison
equal deleted inserted replaced
690:5e8d3f68a997 691:d586ea538d36
2 2
3 """ 3 """
4 JSON explorer 4 JSON explorer
5 """ 5 """
6 6
7 import argparse
7 import json 8 import json
9 import os
8 import sys 10 import sys
9 from pprint import pprint 11 import urllib2
10 12
11 def main(args=sys.argv[1:]): 13 def main(args=sys.argv[1:]):
12 data = sys.stdin.read() # read from stdin
13 obj = json.loads(data)
14 14
15 if args: 15 # command line
16 for arg in args: 16 parser = argparse.ArgumentParser(description='__doc__')
17 foo = arg.split('.') # split into objects 17 parser.add_argument('input', nargs='?',
18 # TODO: split into slice notation 18 help="input file or url (read from stdin if ommitted)")
19 pass # TODO 19 parser.add_argument('object', nargs='*',
20 help="object in dotted notation")
21
22 options = parser.parse_args(args)
23
24 # get data
25 if not options.input or options.input == '-':
26 data = sys.stdin
27 elif'://' in options.input:
28 data = urllib2.urlopen(options.input)
29 else:
30 data = open(options.input, 'r')
31 obj = json.load(data)
32
33 if options.object:
34 for o in options.object:
35 base = obj
36 for part in o.strip().split('.'): # split into objects
37 raise NotImplementedError('TODO')
20 else: 38 else:
21 print json.dumps(obj, indent=2, sort_keys=True) 39 print json.dumps(obj, indent=2, sort_keys=True)
22 40
23 if __name__ == '__main__': 41 if __name__ == '__main__':
24 main() 42 main()