134
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
|
4 JSON explorer
|
|
5 """
|
|
6
|
687
|
7 import argparse
|
134
|
8 import json
|
687
|
9 import os
|
134
|
10 import sys
|
687
|
11 import urllib2
|
134
|
12
|
|
13 def main(args=sys.argv[1:]):
|
687
|
14
|
|
15 # command line
|
|
16 parser = argparse.ArgumentParser(description='__doc__')
|
|
17 parser.add_argument('input', nargs='?',
|
|
18 help="input file or url (read from stdin if ommitted)")
|
|
19 parser.add_argument('object', nargs='*',
|
|
20 help="object in dotted notation")
|
|
21
|
|
22 options = parser.parse_args(args)
|
134
|
23
|
687
|
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:
|
689
|
35 base = obj
|
|
36 for part in o.strip().split('.'): # split into objects
|
|
37 raise NotImplementedError('TODO')
|
134
|
38 else:
|
|
39 print json.dumps(obj, indent=2, sort_keys=True)
|
|
40
|
|
41 if __name__ == '__main__':
|
|
42 main()
|