#!/usr/bin/env python

import sys
import json

# python json_csv.py instances.json Reservations.Instances.PublicDnsName,Reservations.Instances.PrivateDnsName

# {'Reservations': {'Instances': {'PrivateDnsName': {}, 'PublicDnsName': {}}}}

def parse_filter_opts(filter_ops):
    pieces = filter_opts.split(',')
    tree = {}
    for piece in pieces:
        curr_node = tree
        for idx, selector in enumerate(piece.split('.')):
            if selector not in curr_node:
                curr_node[selector] = {}
            if idx == len(piece.split('.')) - 1:
                curr_node[selector]['terminal'] = True
            curr_node = curr_node[selector]
    return tree

# Recurse along both options and trees in parallel
# In case of a list we descend through each child as well
def json_to_csv(json_tree, opts_tree, so_far):
    keys = opts_tree.keys()
    if len(keys) == 1:
        key = keys[0]
        json_tree = json_tree[key]
        if isinstance(json_tree, list):
            for item in json_tree:
                json_to_csv(item, opts_tree[key], so_far)
        else:
            if 'terminal' in opts_tree[key]:
                print json_tree[key]
            else:
                json_to_csv(json_tree, opts_tree[key], so_far)
    else:
        for key in keys:
            if 'terminal' in opts_tree[key]:
                print json_tree[key],
            else:
                # Figure this out
                pass
        print

if __name__ == '__main__':
    filename = sys.argv[1]
    filter_opts = sys.argv[2]

    with open(filename, 'r') as f:
        json_tree = json.loads(f.read())

    opts_tree = parse_filter_opts(filter_opts)

    json_to_csv(json_tree, opts_tree, [])

@@ -677,117 +677,8 @@
 ee%0A%0A
-# Recurse along both options and trees in parallel%0A# In case of a list we descend through each child as well%0A
 def 
@@ -756,73 +756,54 @@
     
-if len(keys) == 1:%0A        key = keys%5B0%5D%0A        json_tree = json
+for key in keys:%0A        if 'terminal' in opts
 _tre
@@ -808,25 +808,29 @@
 ree%5Bkey%5D
-%0A
+:%0A   
         
 if isins
@@ -825,22 +825,15 @@
     
-if isinstance(
+ print 
 json
@@ -841,16 +841,14 @@
 tree
-, list):
+%5Bkey%5D,
 %0A   
@@ -856,107 +856,55 @@
     
-    for item in json_tree:%0A                json_to_csv(item, opts_tree%5Bkey%5D, so_far)%0A        else:%0A
+else:%0A            json_tree = json_tree%5Bkey%5D%0A  
     
@@ -913,98 +913,87 @@
     
-  
 if 
-'terminal' in opts_tree%5Bkey%5D:%0A                print json_tree%5Bkey%5D%0A            else:%0A
+isinstance(json_tree, list):%0A                for item in json_tree:%0A        
     
@@ -996,28 +996,24 @@
             
-    
 json_to_csv(
@@ -1008,33 +1008,28 @@
 json_to_csv(
-json_tree
+item
 , opts_tree%5B
@@ -1046,30 +1046,24 @@
 ar)%0A    
-else:%0A
         
 for key 
@@ -1058,185 +1058,77 @@
     
-for key in keys:%0A            if 'terminal' in opts_tree%5Bkey%5D:%0A                print json_tree%5Bkey%5D,%0A            else:%0A                # Figure this out%0A                pass%0A    
+else:%0A                json_to_csv(json_tree, opts_tree%5Bkey%5D, so_far)%0A
     

Clean up code a little bit