How to Convert CSV to JSON in Python
Use the standard library for repeatable conversions, then jump to the browser tool when you just want the answer once.
Most CSV-to-JSON questions land in one of two camps: you need a script that runs in a pipeline, or you have one file and you want JSON in the next minute. Both are valid; they just call for different tools.
1. Read CSV with DictReader
The standard library has everything you need. csv.DictReader turns each row into a dictionary keyed by the header row, which is already JSON-shaped.
import csv
import json with open('users.csv', newline='', encoding='utf-8') as f: rows = list(csv.DictReader(f)) with open('users.json', 'w', encoding='utf-8') as f: json.dump(rows, f, indent=2)Use newline='' to keep csv from getting confused by Windows line endings, and pass encoding='utf-8' explicitly so the reader does not fall back to a platform default.
2. Cast types when the CSV needs it
CSV stores everything as strings. If your downstream consumer expects numbers or booleans, cast on the way out.
def coerce(row): row['age'] = int(row['age']) row['active'] = row['active'].lower() == 'true' return row rows = [coerce(r) for r in csv.DictReader(f)]3. Handle quoted fields and embedded commas
The csv module already handles double-quoted values, embedded commas, and escaped quotes correctly. If your file uses semicolons or tabs as delimiters, pass delimiter=';' or delimiter='\t'.
When to switch to the browser tool
If you have one CSV and you just want the JSON output without writing a script, use CSV to JSON. Paste the file, copy the JSON, and move on. The page is also useful when you want to verify what your Python script should be producing for a given input.
For the broader topic, see the Data Conversion Tools hub. If your downstream step is to compare two JSON outputs, jump to JSON Diff.