Skip to content

Instantly share code, notes, and snippets.

@beugley
Last active March 14, 2022 00:13
Show Gist options
  • Save beugley/b71784dc96627256cf86336b9b7f5cb2 to your computer and use it in GitHub Desktop.
Save beugley/b71784dc96627256cf86336b9b7f5cb2 to your computer and use it in GitHub Desktop.
Python snippet to convert a json file to csv
import os
import sys
import json
import csv
fin = sys.argv[1]
fout = os.path.splitext(fin)[0]+'.csv'
with open(fin) as f:
j = [json.loads(i) for i in f.readlines()]
with open(fout,"wb") as f:
csvwriter = csv.writer(f, lineterminator = '\n')
csvwriter.writerow(j[0].keys())
for row in j:
csvwriter.writerow(row.values())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment