Skip to content

Instantly share code, notes, and snippets.

@thisismattmiller
Created February 26, 2021 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thisismattmiller/96022ed1bf55e22b1ce6141d93cbc399 to your computer and use it in GitHub Desktop.
Save thisismattmiller/96022ed1bf55e22b1ce6141d93cbc399 to your computer and use it in GitHub Desktop.
jsontocsv.py
import json
import csv
import glob
# we need to know the headers, not all files may have all headers so make a big list first
headers = []
for filename in glob.glob("exhibitions/*.json"):
with open(filename, "r") as data:
json_file = json.load(data)
for key in json_file.keys():
if key not in headers:
headers.append(key)
print(headers)
# make a new writer, using DictWriter
with open('output.csv','w') as outfile:
# pass the headers we know about
writer = csv.DictWriter(outfile, fieldnames=headers)
writer.writeheader()
# loop through the files again
for filename in glob.glob("exhibitions/*.json"):
with open(filename, "r") as data:
json_file = json.load(data)
writer.writerow(json_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment