Skip to content

Instantly share code, notes, and snippets.

@rs77
Created June 2, 2023 22:21
Show Gist options
  • Save rs77/d7ff3659edb97e51252b534c59db562c to your computer and use it in GitHub Desktop.
Save rs77/d7ff3659edb97e51252b534c59db562c to your computer and use it in GitHub Desktop.
Simple example converting JSON data and exporting to CSV file.
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