Created
June 2, 2023 22:21
-
-
Save rs77/d7ff3659edb97e51252b534c59db562c to your computer and use it in GitHub Desktop.
Simple example converting JSON data and exporting 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 | |
json_raw = """ | |
{ | |
"employees": [{ | |
"name": "John Smith", | |
"salary": 120000 | |
},{ | |
"name": "Jane Doe", | |
"salary": 125000 | |
},{ | |
"name": "Mark Twain", | |
"salary": 90000 | |
}] | |
} | |
""" | |
json_data = json.loads(json_raw) | |
csv_data = [] | |
csv_header = ["Employee Name", "Annual Salary"] | |
csv_data.append(csv_header) | |
for employee in json_data['employees']: | |
csv_row = [employee['name'], employee['salary']] | |
csv_data.append(csv_row) | |
with open('output.csv', 'w') as f: | |
csv_writer = csv.writer(f) | |
csv_writer.writerows(csv_data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment