-
-
Save aespar21/c5b1afdb85a3368851d08fc6c99dfcc1 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 hidden or 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