How to convert CSV to JSON
Turn a spreadsheet export into JSON for an API, a script or a config file. How headers become keys, which values become numbers and the edge cases that break.
Spreadsheets hold most of the world’s small datasets and most programs want JSON. Converting between them is simple in principle, headers become keys and rows become objects, but a few details decide whether the result is right.
The basic shape
A CSV with a header row:
name,zip,active,score
Ada,02134,true,91.5
becomes an array of objects:
[{ "name": "Ada", "zip": "02134", "active": true, "score": 91.5 }]
CSV to JSON does this in the browser, from a pasted table or a file and detects comma, semicolon and tab separators.
Types: the part that matters
CSV has no types, so a converter must decide. Converting everything to strings is safe but annoying: code then has to turn “91.5” into a number. Converting everything that looks numeric is worse: ZIP codes, phone numbers and product codes lose their leading zeros and long IDs are rounded. The sensible rule, which the converter uses, is to convert only unambiguous numbers and true or false and leave anything with a leading zero as text.
The same problem appears in spreadsheets, described in CSV vs Excel.
Edge cases that break naive converters
- Commas inside quoted values, such as addresses. A converter that splits on commas shifts every column after them.
- Line breaks inside values, common in notes fields.
- Duplicate or blank headers, which can’t both be JSON keys; they’re renamed with a number suffix.
- A byte order mark at the start of files saved by Excel, which otherwise ends up in the first key’s name.
Checking the result
Paste the output into the JSON formatter to confirm it is valid and see its structure. If something downstream rejects it, how to fix invalid JSON covers the usual errors. And to go the other way, JSON to CSV flattens an array of objects back into a table.
Common questions
What does the JSON look like?
With a header row, you get an array of objects, one per row, with the headers as keys. Without one, you get an array of arrays, one per row.
Why is my ZIP code still a string?
Because 02134 is an identifier, not a number and turning it into 2134 would lose data. Only values that are unambiguously numbers are converted.
What happens to empty cells?
With type conversion on, an empty cell becomes null, which most programs treat as missing. With it off, it stays an empty string.
Can I convert JSON back to CSV?
Yes. JSON to CSV turns an array of objects into rows, using every key that appears as a column, so objects with missing fields still line up.