Skip to content

Instantly share code, notes, and snippets.

@poetix
Created August 29, 2012 09:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save poetix/3509524 to your computer and use it in GitHub Desktop.
Save poetix/3509524 to your computer and use it in GitHub Desktop.
Short script to fetch JSON from MongoDB, flatten to dictionary of leaf nodes, and write everything out in CSV format
#!/usr/bin/python
import urllib2
import argparse
import json
parser = argparse.ArgumentParser(description="Fetch some JSON")
parser.add_argument("url")
args = parser.parse_args()
response = urllib2.urlopen(args.url)
json = json.loads(response.read())
first_row = json["rows"][0]
def traverse(o):
for key, value in o.iteritems():
if isinstance(value, dict):
for key, value in traverse(value):
yield (key, value)
else:
yield (key, value)
all_keys = list((key for key, value in traverse(first_row)))
print(",".join(all_keys))
for row in json["rows"]:
data = dict(traverse(row))
values = (str(data[key]) for key in all_keys)
print(",".join(values))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment