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.
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
HTML Entity Encoder
Encode and decode HTML entities, special characters, and symbols
Image to Base64
Convert images to Base64 data URIs or decode Base64 back to images
JSON to TypeScript
Generate TypeScript interfaces and type aliases from JSON data
HTML ↔ Markdown
Convert between HTML and Markdown in either direction