How to Convert CSV to JSON (and Back): A Practical Guide
Published 2026-05-28
CSV and JSON are the two formats most data ends up in. CSV (Comma-Separated Values) is what spreadsheets export and what analysts live in; JSON (JavaScript Object Notation) is what APIs speak and what web applications consume. Sooner or later you need to move data from one world to the other — importing a price list into an app, feeding a spreadsheet to a script, or flattening an API response so a colleague can open it in Excel. This guide explains exactly how the conversion works and where it commonly goes wrong.
How CSV maps to JSON
The mental model is simple: the first row of a CSV becomes the keys, and every following row becomes one JSON object. Take this CSV:
name,age,city
Alice,30,Paris
Bob,25,Berlin
It converts to an array of two objects, one per data row:
[
{ "name": "Alice", "age": "30", "city": "Paris" },
{ "name": "Bob", "age": "25", "city": "Berlin" }
]
Notice that the numbers came across as strings ("30", not 30). CSV has no type information — everything is text — so a strict converter keeps values as strings unless you explicitly ask it to infer numbers and booleans. That ambiguity is the single biggest source of surprises when you convert data.
The quoting rule everyone forgets
What happens when a value itself contains a comma? Consider an address like Paris, France. If you wrote it raw, the parser would see two columns instead of one. The CSV standard solves this by wrapping such values in double quotes:
name,location
Alice,"Paris, France"
And if a quoted value needs to contain a literal double quote, you escape it by doubling it — for example "She said ""hello"" loudly" represents the text: She said "hello" loudly. A correct converter respects both rules; a naive split-on-comma does not, which is why hand-rolled CSV parsing breaks on real-world data. Always use a proper parser.
Converting JSON back to CSV
The reverse direction has its own catch: JSON can be nested, and CSV cannot. A flat array of flat objects converts cleanly — each key becomes a column. But if an object contains a nested object or an array, there is no universally correct way to flatten it into a single cell. The common strategies are:
- Flatten with dotted keys:
address.citybecomes its own column. - Serialize the nested part back into a JSON string inside one cell.
- Pick the fields you need and ignore the rest.
For most spreadsheet work, flattening or selecting specific fields gives the cleanest result. Decide before you convert, not after.
Common pitfalls
- Disappearing leading zeros. A postal code like
01234becomes1234when a spreadsheet treats it as a number. This is an Excel behavior, not a conversion bug — format the column as text on import to keep the zeros. - Encoding problems. Accented characters turn into mojibake when the file is not read as UTF-8. Save and open your CSV as UTF-8 to keep
é,ü, and other characters intact. - Inconsistent rows. If some rows have more columns than the header, the extra values have no key. Clean the source data first.
- Date formats. Dates are just text in CSV. Standardize on ISO format (
2026-05-28) before converting to avoid locale confusion between day/month order.
Doing it safely in your browser
Data files often contain things you would rather not upload — customer emails, financial figures, internal IDs. A converter that runs entirely in your browser processes the file locally and never transmits it to a server, which makes it safe even for confidential exports. Paste your CSV or JSON, pick the direction, and copy the result.