Convert JSON to CSV in Python with pandas or the csv Module

Published 24 November 2026 · 7 min read

Python gives you two good ways to turn JSON into CSV: pandas, which is shortest and handles nesting, and the built-in csv module, which needs no install. Both are below, with the problems that usually bite afterwards.

With pandas

If the JSON is an array of objects, this is all you need:

import json
import pandas as pd

with open("data.json", encoding="utf-8") as f:
    data = json.load(f)

df = pd.json_normalize(data)
df.to_csv("out.csv", index=False, encoding="utf-8-sig")

json_normalize flattens nested objects into columns joined with a dot, such as address.city. The utf-8-sig encoding adds a byte order mark so Excel opens accented characters correctly.

Arrays inside records

A list inside each record does not become columns. To get one row per list item, name the list with record_path and keep parent fields with meta:

df = pd.json_normalize(
    data,
    record_path="items",
    meta=["order_id", "customer"],
)

The parent values repeat on each row. This is the same choice described in flatten nested JSON into CSV: one row per item, or the list squeezed into one cell.

With only the standard library

import csv, json

with open("data.json", encoding="utf-8") as f:
    rows = json.load(f)

fields = []
for row in rows:
    for key in row:
        if key not in fields:
            fields.append(key)

with open("out.csv", "w", newline="", encoding="utf-8-sig") as f:
    w = csv.DictWriter(f, fieldnames=fields)
    w.writeheader()
    w.writerows(rows)

Collecting the keys from every row, not just the first, avoids losing columns when records differ. Always open the file with newline="", or Windows adds blank lines between rows. Nested values are written as Python text, so flatten them first.

JSON Lines

For one JSON object per line, pandas reads it directly:

df = pd.read_json("data.jsonl", lines=True)

For files too large for memory, pass chunksize and write each chunk in append mode. See JSONL vs JSON for the format.

Problems to watch for

  • Long IDs. Python reads big integers exactly, but Excel shows only 15 significant digits when it opens the CSV. Excel is the one that changes them; see why Excel changes long numbers.
  • pandas guesses types. A column of IDs with some blanks can become floats, so 123 turns into 123.0. Read with dtype=str if you need exact text, or use the csv module.
  • Delimiters. European Excel expects semicolons. See CSV in one column or garbled.

When you do not need code

For a one-off file, the JSON to CSV converter does the same flattening in your browser, with a preview, and nothing is uploaded.