changeset 692:80c47cc35f37

to method
author Jeff Hammel <k0scist@gmail.com>
date Mon, 02 Jun 2014 13:49:46 -0700
parents d586ea538d36
children c7309e7625ba
files python/jsonex.py
diffstat 1 files changed, 13 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/python/jsonex.py	Mon Jun 02 12:40:51 2014 -0700
+++ b/python/jsonex.py	Mon Jun 02 13:49:46 2014 -0700
@@ -10,6 +10,13 @@
 import sys
 import urllib2
 
+def dereference(obj, key):
+    if obj is None:
+        return None
+    if isinstance(obj, dict):
+        return obj[key]
+    return obj[int(key)]
+
 def main(args=sys.argv[1:]):
 
     # command line
@@ -33,10 +40,13 @@
     if options.object:
         for o in options.object:
             base = obj
-            for part in o.strip().split('.'): # split into objects
-                raise NotImplementedError('TODO')
+            for key in o.strip().split('.'): # split into objects
+                base = dereference(base, key)
+                if base is None:
+                   break
+            print (json.dumps(base))
     else:
-        print json.dumps(obj, indent=2, sort_keys=True)
+        print (json.dumps(obj, indent=2, sort_keys=True))
 
 if __name__ == '__main__':
     main()