CSV to JSON (and Back): A Developer's Conversion Guide
Converting between CSV and JSON sounds trivial until you hit quoted commas, nested fields, and type inference. Here's the practical guide — plus a free browser tool.
CSV and JSON are the two dominant data-interchange formats, and you'll eventually need to translate between them. For anything more than 100 rows, hand-coding the conversion is a waste of time. For anything sensitive, pasting into a random online converter is a waste of trust. This guide covers both directions, the edge cases that break naive implementations, and how to do it privately in a browser.
When to use CSV vs JSON
CSV is ideal for tabular data — one type of record, repeated. Spreadsheets, database exports, analytics dumps. It's compact and human-readable in Excel.
JSON is ideal for nested or heterogeneous data — API responses, config files, documents with optional fields. It's native to JavaScript and easy to parse in every language.
If your data is "a list of uniform records," either works. If it's "a tree," CSV is the wrong tool.
CSV → JSON, the simple case
name,email,age
Alice,[email protected],30
Bob,[email protected],25
Becomes:
[
{ "name": "Alice", "email": "[email protected]", "age": 30 },
{ "name": "Bob", "email": "[email protected]", "age": 25 }
]
Every CSV row becomes one object; the header row becomes the keys. That's the 80% case.
Where naive converters fall over
- Commas inside quoted fields.
"Doe, John",42has two fields, not three. A regex-split on comma wrecks this. Proper converters tokenize character-by-character. - Escaped quotes. The CSV spec uses
""(two consecutive double-quotes) to mean a literal quote inside a quoted field. - Line breaks inside fields. Quoted strings in CSV can contain newlines. A line-based parser splits them into garbage.
- Type inference. Is
007a string (zip-code prefix) or a number (James Bond)? IsTRUEthe boolean or a product code? Good converters leave values as strings unless you opt into inference. - UTF-8 BOM. Excel often exports CSV with a UTF-8 byte-order mark. Naive parsers see the first column name as
nameinstead ofname.
Our CSV to JSON converter handles all five correctly. It's client-side (your data doesn't leave the browser) and handles arbitrarily large files as long as your browser has the RAM.
JSON → CSV, the simple case
An array of flat objects becomes a table. The keys of the first object become the header; each object becomes a row. JSON to CSV does this directly.
JSON → CSV, the nested case
Here's where it gets interesting. Given:
[
{ "name": "Alice", "address": { "city": "Paris", "zip": "75001" } },
{ "name": "Bob", "address": { "city": "Lyon", "zip": "69001" } }
]
You have two reasonable choices:
- Flatten with dotted keys. Columns:
name,address.city,address.zip. Readable, reversible. - Stringify the nested object. Column
addresscontains the JSON string{"city":"Paris",...}. Lossy for spreadsheets but preserves structure.
Our tool defaults to flattening. If you need the stringify behavior (or the raw "drop nested columns" behavior), tell us and we'll expose the option.
Arrays within objects
Arrays ("tags": ["red", "blue"]) typically get joined with a separator (red|blue). This is lossy if your values contain the separator — choose a rarely-used one (pipe, semicolon).
Delimiters besides the comma
Despite the name, "CSV" files often use semicolons (European locales where comma is the decimal mark) or tabs (TSV). Real tools detect the delimiter automatically or let you override it.
Encoding matters
Always emit UTF-8. Always include a BOM if the file will be opened in Excel on Windows (Excel assumes legacy encoding otherwise and mangles accented characters). Our tool handles this automatically.
Example: cleaning an export
A common workflow:
- Export a database table to JSON
- Convert to CSV for a non-technical colleague
- They edit in Excel
- You convert back to JSON for your API
Round-tripping is where encoding and delimiter choice matter most — always agree on UTF-8 and comma-delimited before you start.