Skip to content

Instantly share code, notes, and snippets.

@jones77
Created September 5, 2017 17:51
Show Gist options
  • Select an option

  • Save jones77/db08ce9465712bd9f01a5be6fd570d28 to your computer and use it in GitHub Desktop.

Select an option

Save jones77/db08ce9465712bd9f01a5be6fd570d28 to your computer and use it in GitHub Desktop.
Flatten json to csv
#!/usr/bin/env python3
# usage: json2csv url
# requirements: requests, flatten_json
# ie use pip or pip2 and install them:
# sudo pip (or sudo pip2) install requests flatten_json
import sys
from flatten_json import flatten
import requests
if __name__ == '__main__':
r = requests.get(sys.argv[1])
d = r.json()
f = flatten(d)
# FIXME: escape quotes?
# Headings
print("{}".format(','.join(f.keys())))
# Values
print("{}".format(','.join(f.values())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment