Back to Python
2025-12-157 min read

JSON to CSV (Python Programming)

Learn JSON to CSV (Python Programming) step by step with clear examples and exercises.

Why This Matters

When working with data, it is essential to convert between various formats to facilitate easier analysis and storage. JSON (JavaScript Object Notation) is a popular format for handling data structures, while CSV (Comma Separated Values) is commonly used for tabular data in spreadsheets. Python offers several libraries to help you convert JSON to CSV quickly and efficiently. In this lesson, we'll explore how to use the json and csv modules to perform this conversion.

Why This Matters

Data manipulation is an integral part of any data analysis or processing task. Converting between different data formats allows for seamless integration of data from various sources. JSON is a versatile format that can represent complex data structures, while CSV is simple and easy to read and write. Understanding how to convert JSON to CSV in Python will enable you to work with data more efficiently and effectively.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of:

  1. Python programming concepts (variables, functions, loops, etc.)
  2. JSON data structures
  3. CSV files and their structure
  4. File handling in Python

If you're not familiar with these topics, consider reviewing them before proceeding.

Core Concept

To convert a JSON object to a CSV file using Python, follow these steps:

  1. Import the necessary libraries (json and csv)
  2. Load your JSON data
  3. Create a CSV writer object
  4. Iterate through the JSON data and write it to the CSV file
  5. Save the CSV file

Here's an example of how this can be done:

import json
import csv

Sample JSON data (dictionary)

data = {

"name": "John Doe",

"age": 30,

"city": "New York"

}

Open a CSV file for writing

with open('output.csv', 'w', newline='') as csvfile:

Create a CSV writer object

fieldnames = data.keys()

writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

Write the header (field names)

writer.writeheader()

Write the JSON data to the CSV file

writer.writerow(data)


In this example, we first import the required libraries and define a sample JSON object. We then open a new CSV file for writing using the `open()` function. Inside the context manager, we create a `DictWriter` object that will handle writing our data as rows with field names as headers. We specify the fieldnames by getting the keys from the JSON object.

Next, we write the header (field names) to the CSV file using the `writeheader()` method. Finally, we write the JSON data to the CSV file using the `writerow()` method and pass our JSON object as an argument. The resulting CSV file will look like this:

name,age,city

John Doe,,New York

Core Concept (Expanded)

In more complex scenarios, you may have a JSON file containing multiple objects or nested structures. To handle such cases, you can modify the code to read and write data from/to files instead of using in-memory JSON objects. Here's an example:

import json
import csv

Load the JSON data from a file

with open('data.json', 'r') as f:

data = json.load(f)

Open a CSV file for writing

with open('output.csv', 'w', newline='') as csvfile:

Create a CSV writer object

fieldnames = ['name', 'age', 'city']

writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

Write the header (field names)

writer.writeheader()

Iterate through the JSON data and write it to the CSV file

for item in data:

writer.writerow(item)


In this example, we load the JSON data from a file named `data.json`. We then create a CSV writer object as before but with a predefined list of field names. We iterate through the JSON data using a for loop and write each item to the CSV file using the `writerow()` method.

Worked Example

Let's work through a more complex example involving multiple JSON objects and nested structures. We'll use a sample JSON file containing data about books:

[
{
"title": "The Catcher in the Rye",
"author": "J.D. Salinger",
"year": 1951,
"genre": ["Fiction", "Literature"]
},
{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"year": 1960,
"genre": ["Fiction", "Literature", "Drama"]
}
]

Here's the Python code to convert this JSON data into a CSV file:

import json
import csv

Load the JSON data from a file

with open('books.json', 'r') as f:

data = json.load(f)

Open a CSV file for writing

with open('output.csv', 'w', newline='') as csvfile:

Create a CSV writer object

fieldnames = ['title', 'author', 'year', 'genre']

writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

Write the header (field names)

writer.writeheader()

Iterate through the JSON data and write it to the CSV file

for book in data:

writer.writerow({book, {'genre': ', '.join(book['genre'])}})


In this example, we load the JSON data from a file named `books.json`. We then create a CSV writer object as before but with a predefined list of field names. To handle the nested structure (i.e., the list of genres), we use the `**` operator to merge the book dictionary and another dictionary containing the comma-separated genres.

Worked Example

Let's further expand on the worked example by handling a more complex JSON structure:

[
{
"title": "The Catcher in the Rye",
"author": "J.D. Salinger",
"year": 1951,
"genre": ["Fiction", "Literature"],
"awards": [
{"name": "National Book Award", "year": 1952},
{"name": "Library of Congress Living Legends", "year": 1984}
]
},
{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"year": 1960,
"genre": ["Fiction", "Literature", "Drama"],
"awards": [
{"name": "Pulitzer Prize", "year": 1961}
]
}
]

Here's the Python code to convert this JSON data into a CSV file:

import json
import csv

Load the JSON data from a file

with open('books.json', 'r') as f:

data = json.load(f)

Open a CSV file for writing

with open('output.csv', 'w', newline='') as csvfile:

Create a CSV writer object

fieldnames = ['title', 'author', 'year', 'genre'] + [f'award_{i}' for i in range(len(data[0]["awards"]))]

writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

Write the header (field names)

writer.writeheader()

Iterate through the JSON data and write it to the CSV file

for book in data:

writer.writerow({book, {f'award_{i}': award['name'] + (', ' if i < len(data[0]["awards"]) - 1 else '') for i, award in enumerate(book['awards'])}})


In this example, we handle the nested structure of awards by dynamically generating field names using a list comprehension. We then iterate through the awards and write them to the CSV file as a comma-separated string.

Common Mistakes

1. Forgetting to import the necessary libraries (json and csv)

Ensure you have imported both json and csv modules at the beginning of your script.

2. Not defining fieldnames correctly

Make sure that the field names match the keys in your JSON data or specify appropriate keys if the structure varies.

3. Incorrectly opening/closing the CSV file

Always use a context manager (with open()) to handle file operations and ensure you close the file after writing the data.

4. Not handling nested structures properly

When dealing with nested structures, make sure to iterate through each level of nesting and write the relevant data to the CSV file. You may need to use recursive methods or modify your code to accommodate specific structures.

Practice Questions

  1. Given the following JSON object, convert it to a CSV file:
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
}
]
}
  1. You have a JSON file containing data about products, and you want to convert it into a CSV file. The JSON structure looks like this:
[
{
"id": 1,
"name": "Product A",
"price": 50,
"category": "Electronics"
},
{
"id": 2,
"name": "Product B",
"price": 100,
"category": "Furniture"
}
]

Write the Python code to create a CSV file with the following headers: ID, Name, Price, and Category.

FAQ

Q: Can I use pandas to convert JSON to CSV?

A: Yes, you can use pandas' DataFrame functions to easily convert JSON data into a CSV file. However, for the purpose of this lesson, we focused on using the built-in json and csv modules.

Q: How do I handle nested structures in my JSON data when converting to CSV?

A: To handle nested structures in your JSON data, you can use recursive methods or modify your code to accommodate the specific structure of your data. For example, if your JSON data contains a list of objects with multiple levels of nesting, you might need to iterate through each level and write the relevant data to the CSV file.

Q: Can I convert a JSON object directly to a CSV string without writing it to a file?

A: Yes, you can convert a JSON object to a CSV string using libraries like csv or pandas. However, for simplicity's sake, we focused on writing the data to a file in this lesson. If you want to convert the data to a string, you can open a temporary file and read its contents after writing the data.

JSON to CSV (Python Programming) | Python | XQA Learn