Converting JSON to CSV with Python

Simple approach to convert JSON to csv


2 min read

JSON (JavaScript Object Notation) is a popular data format used for storing data in a structured manner. CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database.

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 json and 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[0].keys())
    # Write the header row
    # Write the data rows
    for row in data:

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!