Created
August 29, 2012 09:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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