Created
October 5, 2020 19:49
-
-
Save psrdotcom/f381f13ffa2006f6645800d6c3b9894f to your computer and use it in GitHub Desktop.
Convert JSON object array to CSV File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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