Skip to content

Instantly share code, notes, and snippets.

@thinkjson
Created May 19, 2012 01:36
Show Gist options
  • Save thinkjson/2728511 to your computer and use it in GitHub Desktop.
Save thinkjson/2728511 to your computer and use it in GitHub Desktop.
JSON to CSV conversion benchmarking
json2csv.py
real 5m28.824s
user 5m27.584s
sys 0m1.524s
C (https://github.com/jehiah/json2csv)
real 0m15.945s
user 0m15.181s
sys 0m1.204s
import json
import csv
import sys
fields = sys.argv[1].split(",")
output = csv.writer(sys.stdout, delimiter=',', quotechar='"')
for line in sys.stdin:
if line.strip() != "":
try:
obj = json.loads(line)
row = []
for field in fields:
if field in obj:
row.append(obj[field])
else:
row.append(None)
output.writerow(row)
except ValueError:
sys.stderr.write("Error: %s" % line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment