Skip to content

Instantly share code, notes, and snippets.

@nemith
Created June 14, 2014 04:46
Show Gist options
  • Select an option

  • Save nemith/1182437e248b8b69d17a to your computer and use it in GitHub Desktop.

Select an option

Save nemith/1182437e248b8b69d17a to your computer and use it in GitHub Desktop.
import json
import csv
import cStringIO
JSON = '''
{"items": [
{"name": "Test One", "integer": 45},
{"name": "Test Two", "integer": 76},
{"name": "Test Three", "integer": 42}
]
}
'''
data = json.loads(JSON)
items = data['items']
output = cStringIO.StringIO()
writer = csv.DictWriter(output, items[0].keys())
writer.writeheader()
for row in items:
writer.writerow(row)
print output.getvalue()
bbennett@annav-t420s:~<1>$ python jsontest.py
integer,name
45,Test One
76,Test Two
42,Test Three
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment