comparison python/jsonex.py @ 692:80c47cc35f37

to method
author Jeff Hammel <k0scist@gmail.com>
date Mon, 02 Jun 2014 13:49:46 -0700
parents 6f48a3e5ab41
children 0fa456446fea
comparison
equal deleted inserted replaced
691:d586ea538d36 692:80c47cc35f37
7 import argparse 7 import argparse
8 import json 8 import json
9 import os 9 import os
10 import sys 10 import sys
11 import urllib2 11 import urllib2
12
13 def dereference(obj, key):
14 if obj is None:
15 return None
16 if isinstance(obj, dict):
17 return obj[key]
18 return obj[int(key)]
12 19
13 def main(args=sys.argv[1:]): 20 def main(args=sys.argv[1:]):
14 21
15 # command line 22 # command line
16 parser = argparse.ArgumentParser(description='__doc__') 23 parser = argparse.ArgumentParser(description='__doc__')
31 obj = json.load(data) 38 obj = json.load(data)
32 39
33 if options.object: 40 if options.object:
34 for o in options.object: 41 for o in options.object:
35 base = obj 42 base = obj
36 for part in o.strip().split('.'): # split into objects 43 for key in o.strip().split('.'): # split into objects
37 raise NotImplementedError('TODO') 44 base = dereference(base, key)
45 if base is None:
46 break
47 print (json.dumps(base))
38 else: 48 else:
39 print json.dumps(obj, indent=2, sort_keys=True) 49 print (json.dumps(obj, indent=2, sort_keys=True))
40 50
41 if __name__ == '__main__': 51 if __name__ == '__main__':
42 main() 52 main()