Python CSV to JSON Converter
Convert CSV data to JSON for your Python projects. Paste CSV below to convert it instantly, then use the Python code examples with csv.DictReader or pandas in your application. All conversion is client-side.
CSV ↔ JSON Converter
Convert between CSV and JSON formats. Handles quoted fields, custom delimiters, and nested commas.
About CSV ↔ JSON Conversion
- Handles quoted fields with commas, newlines, and escaped quotes (
""). - Supports custom delimiters: comma, tab, semicolon, and pipe.
- CSV → JSON uses the first row as object keys (headers).
- JSON → CSV flattens objects into columns — nested objects are stringified.
- Everything runs in your browser — no data is sent over the network.
How to convert CSV to JSON in Python
Using the csv module: import csv, json; with open('data.csv') as f: reader = csv.DictReader(f); rows = list(reader); with open('data.json', 'w') as f: json.dump(rows, f, indent=2). DictReader automatically uses the first row as keys. For custom delimiters: csv.DictReader(f, delimiter=';'). One-liner with pandas: import pandas as pd; pd.read_csv('data.csv').to_json('data.json', orient='records', indent=2). The orient='records' option produces a JSON array of objects — the most common format for APIs.
# Python — convert CSV to JSON
import csv
import json
# CSV string → JSON
csv_data = """name,age,city
Alice,30,New York
Bob,25,London"""
import io
reader = csv.DictReader(io.StringIO(csv_data))
json_data = json.dumps(list(reader), indent=2)
print(json_data)
# [
# { "name": "Alice", "age": "30", "city": "New York" },
# { "name": "Bob", "age": "25", "city": "London" }
# ]
# CSV file → JSON file
with open("data.csv") as f:
rows = list(csv.DictReader(f))
with open("data.json", "w") as f:
json.dump(rows, f, indent=2)Handling CSV encoding and edge cases in Python
Encoding: open('data.csv', encoding='utf-8-sig') handles BOM-prefixed Excel CSVs. For unknown encoding: import chardet; encoding = chardet.detect(open('data.csv', 'rb').read())['encoding']. Missing values: csv.DictReader returns empty strings; pandas returns NaN. Handle nulls: json.dumps(data, default=str) converts non-serializable values to strings. Large files: use chunked reading: for chunk in pd.read_csv('large.csv', chunksize=10000): process(chunk). Type inference: pandas auto-detects integers, floats, and dates; csv module returns everything as strings.
JSON to CSV conversion in Python
Reverse conversion with pandas: pd.read_json('data.json').to_csv('output.csv', index=False). With csv module: import csv, json; data = json.load(open('data.json')); writer = csv.DictWriter(open('output.csv', 'w', newline=''), fieldnames=data[0].keys()); writer.writeheader(); writer.writerows(data). For nested JSON, flatten first: pd.json_normalize(data) handles nested objects by creating dot-separated column names (e.g., 'address.city').
Frequently Asked Questions
How do I convert CSV to JSON in Python without pandas?
Use csv.DictReader: import csv, json; data = list(csv.DictReader(open('file.csv'))); json.dump(data, open('output.json', 'w'), indent=2). DictReader uses headers as keys and returns an ordered dict for each row. No external dependencies needed.
How do I handle large CSV files in Python?
Process in chunks: for chunk in pd.read_csv('large.csv', chunksize=10000): result = chunk.to_dict('records'); process_batch(result). With csv module, iteration is already lazy: for row in csv.DictReader(f) reads one row at a time. Avoid list(reader) on large files as it loads everything into memory.
How do I convert nested JSON to CSV in Python?
Use pandas json_normalize: pd.json_normalize(data, sep='_').to_csv('output.csv'). This flattens nested objects into dot-separated columns (e.g., 'address.city' becomes 'address_city'). For deeply nested structures, specify the record_path and meta parameters.
Related Convert Tools
SQL to TypeScript/Prisma/Drizzle
Convert SQL CREATE TABLE statements to TypeScript interfaces, Prisma schema, and Drizzle ORM definitions for PostgreSQL, MySQL, and SQLite
ESLint to Biome Converter
Convert your ESLint config to Biome — maps 100+ rules from core, TypeScript, React, JSX-A11y, and import plugins to biome.json
Tailwind to CSS Converter
Convert Tailwind CSS utility classes to standard CSS — supports 500+ classes including spacing, layout, typography, transforms, and arbitrary values
.env to Docker/K8s Converter
Convert .env files to Docker Compose environment blocks, Kubernetes ConfigMaps, Secrets, and docker run flags with sensitive key detection