How to Convert CSV to JSON — Online, Python, Node.js, and Best Practices
Learn how to convert CSV (comma-separated values) to JSON in the browser, Python, Node.js, and Go. Covers header-mapping, nested-object flattening, and handling CSV quirks like embedded commas and quotes.
Last updated:
Core Concepts
- CSV → JSON: the basic transformation
- CSV is a table (rows × columns), and JSON is a tree. The simplest conversion maps each CSV row to a JSON object, with column headers as keys. Row 1 ('name','age','city') becomes the key names; Row 2 ('Alice',30,'NYC') becomes the first object: {"name":"Alice","age":30,"city":"NYC"}. The output is a JSON array of objects.
- Handling CSV edge cases
- CSV is deceptively tricky. Common gotchas: 1) Embedded commas — 'New York, NY' inside a quoted field: the parser must ignore the comma between quotes. 2) Embedded quotes — doubled quotes ("") inside a quoted field. 3) Inconsistent column counts — some rows have more or fewer fields than the header. 4) BOM (byte order mark) at the start of UTF-8 CSV files from Excel. A good converter handles all four.
- Flat JSON vs nested JSON
- Simple conversion produces a flat array of flat objects — perfect for most use cases. But sometimes you want nested structures: {'user.name':'Alice','user.age':30,'address.city':'NYC'} → {"user":{"name":"Alice","age":30},"address":{"city":"NYC"}}. Our converter supports dot-notation unflattening for exporting to APIs that expect nested payloads.
Frequently Asked Questions
Can I convert CSV to JSON in Python?
Yes: import csv; import json; with open('data.csv') as f: reader = csv.DictReader(f); print(json.dumps(list(reader))). This one-liner does exactly what our online converter does. For large files (100MB+), use ijson for streaming output instead of loading everything into memory.
What if my CSV uses a different delimiter?
TSV (tab-separated), pipe-delimited (|), and semicolon-delimited (;) are all valid. Most converters auto-detect the delimiter by looking at the first line. Our converter lets you specify the delimiter explicitly if auto-detection picks the wrong one.
Try these related tools
CSV → JSON →
Convert CSV data to a JSON array of objects online. Auto-detects delimiter and quotes, preserves numeric types when requested.
JSON → CSV →
Convert an array of JSON objects to CSV online. Headers are inferred automatically from all keys across records.
JSON Flatten →
Flatten nested JSON to a single-level 'path → value' map (dot / bracket notation), or unflatten a flat map back into nested JSON. Compatible with lodash and Spring conventions. 100% local.
JSON Formatter →
Format, beautify, and validate JSON online. Free, fast, and 100% local — your data never leaves your browser.