Skip to content

Instantly share code, notes, and snippets.

@psrdotcom
Created October 5, 2020 19:49
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 psrdotcom/f381f13ffa2006f6645800d6c3b9894f to your computer and use it in GitHub Desktop.
Save psrdotcom/f381f13ffa2006f6645800d6c3b9894f to your computer and use it in GitHub Desktop.
Convert JSON object array to CSV File
import json
import csv
# Read JSON File
with open('users.json') as json_file:
data = json.load(json_file)
user_data = data['users']
# Prepare CSV file
data_file = open('users.csv', 'w')
# Make CSV file writer
csv_writer = csv.writer(data_file)
count = 0
for tmp in user_data:
if count == 0:
# Add headers to CSV file
header = tmp.keys()
csv_writer.writerow(header)
count += 1
# Write data to CSV file
csv_writer.writerow(tmp.values())
# Close the CSV file
data_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment