Photo by Ferenc Almasi on Unsplash
Converting JSON to CSV with Python
Simple approach to convert JSON to csv
2 min read
If you have data stored in a JSON file that you want to use in a spreadsheet or other data analysis tool, you can convert the JSON file to CSV using Python. This can be done using the
csv modules, which are included in the Python standard library.
Here's a simple Python script that converts a JSON file to CSV:
import json import csv # Open the JSON file and load the data with open("data.json", "r") as f: data = json.load(f) # Open a new CSV file for writing with open("data.csv", "w") as f: # Create a CSV writer writer = csv.DictWriter(f, fieldnames=data.keys()) # Write the header row writer.writeheader() # Write the data rows for row in data: writer.writerow(row)
This script reads in a JSON file called
data.json, converts the data to a list of dictionaries, and then writes this data to a CSV file called
data.csv. The field names for the CSV file are taken from the keys in the first dictionary in the list.
You can customize this script to fit your specific needs. For example, you could add command-line arguments to allow the user to specify the input and output filenames, or you could add error handling to handle cases where the JSON file is improperly formatted.
Did you find this article valuable?
Support Chop Coding by becoming a sponsor. Any amount is appreciated!