The Example Data
All examples use this file, users.json: a list of objects.
[
{ "id": 1, "name": "Emily Clark", "city": "London", "orders": 12 },
{ "id": 2, "name": "James Smith", "city": "Manchester", "orders": 3 },
{ "id": 3, "name": "Sara Ahmed", "city": "Leeds", "orders": 7 }
]No Code? Convert Online
For a one-off file, paste it into the JSON to CSV converter, or download a real spreadsheet with JSON to Excel. Both run in your browser, so the data is never uploaded.
Method 1: pandas (Easiest)
import pandas as pd # pip install pandas
df = pd.read_json("users.json")
df.to_csv("users.csv", index=False)index=False stops pandas from adding an extra column of row numbers.
Method 2: The Built-in csv Module (No Install)
import csv, json
with open("users.json", encoding="utf-8") as f:
rows = json.load(f)
with open("users.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=rows[0].keys())
writer.writeheader()
writer.writerows(rows)Always open the CSV file with newline="", otherwise Windows adds an empty line between rows.
When Objects Have Different Keys
If some objects have extra fields, collect every key first, or DictWriter raises a ValueError.
fieldnames = []
for row in rows:
for key in row:
if key not in fieldnames:
fieldnames.append(key)
writer = csv.DictWriter(f, fieldnames=fieldnames, restval="")Method 3: Nested JSON
CSV is flat, so nested objects must be flattened into columns like address.city. pandas does this with json_normalize.
import json
import pandas as pd
data = [
{"id": 1, "name": "Emily", "address": {"city": "London", "zip": "SW1A"}},
{"id": 2, "name": "James", "address": {"city": "Leeds", "zip": "LS1"}},
]
df = pd.json_normalize(data) # columns: id, name, address.city, address.zip
df.to_csv("users.csv", index=False)If the list you want is inside a bigger object, point to it with record_path, for example pd.json_normalize(data, record_path="users").
Opening the CSV in Excel
Excel sometimes shows accented letters wrongly in a UTF-8 CSV. Save with encoding="utf-8-sig" to fix it:
df.to_csv("users.csv", index=False, encoding="utf-8-sig")Only need a spreadsheet once? The JSON to Excel guide shows ways without Python. Not sure CSV is the right format? Read JSON vs CSV.
Common Errors
- ValueError: Trailing data (pandas): the file is JSON Lines, one object per line. Use
pd.read_json("file.json", lines=True). - json.decoder.JSONDecodeError: the JSON itself is invalid. Check it with the JSON Validator.
- dict contains fields not in fieldnames: some rows have extra keys. Collect all keys first, as shown above.
Frequently Asked Questions
What is the easiest way to convert JSON to CSV in Python?
Use pandas: df = pd.read_json('file.json') and then df.to_csv('file.csv', index=False).
Can I convert JSON to CSV without pandas?
Yes. Load the file with json.load and write it with csv.DictWriter from the built-in csv module.
How do I convert nested JSON to CSV?
Use pandas.json_normalize, which flattens nested objects into columns like address.city.
Why does my CSV have blank lines in Excel?
Open the output file with newline='' when using the csv module. To show accented letters correctly in Excel, save with encoding='utf-8-sig'.