JSON & Data
How to Convert CSV to JSON (With Proper Header Detection)
Turn CSV rows into a clean JSON array of objects with automatic header detection. Learn parsing rules, quoting, type handling, and common pitfalls.
Try the toolCSV to JSON →Someone hands you a CSV export — a report from Sheets, a database dump, a list of records — and your code wants JSON. CSV is great for spreadsheets but painful to consume programmatically: no types, no structure, just delimited text. Converting to a JSON array of objects gives each row named fields you can iterate, filter, and feed straight into an API.
How the conversion works
The standard mapping uses the first row as the header. Each subsequent row becomes an object whose keys are those headers and whose values are the cells in order:
id,name,role
1,Ada Lovelace,admin
2,Alan Turing,editor
becomes:
[
{ "id": "1", "name": "Ada Lovelace", "role": "admin" },
{ "id": "2", "name": "Alan Turing", "role": "editor" }
]
Header detection is what makes the output useful — without it you'd get positional arrays like ["1", "Ada Lovelace", "admin"] that tell you nothing about what each column means.
Why you can't just split on commas
The naive approach — line.split(',') — breaks the moment a value contains a comma, a quote, or a newline. Real CSV quotes those fields, and quotes inside a quoted field are doubled:
name,note
"Wright, Orville","Said ""hello"""
"Multi
line",ok
Here row one has a comma inside Wright, Orville and an escaped quote in the note; row two has a literal newline inside a quoted cell. A correct parser walks the text character by character, tracking whether it's inside quotes, so it never splits in the wrong place. This is why using a real CSV parser beats a regex every time.
A practical walkthrough
Paste your CSV into the CSV to JSON tool. It parses locally in your browser, treats the first row as headers, and emits a clean JSON array of objects you can copy into your code.
- Paste or drop your CSV text.
- Confirm the first row is being read as the header.
- Review the JSON array output.
- Copy it into your app, request body, or test fixture.
Types: everything starts as a string
CSV has no notion of type — 42, true, and 2026-07-15 are all just text. By default a safe converter keeps values as strings, which avoids surprises. If you need real numbers or booleans, coerce them explicitly in your code so you control the rules:
const rows = parsed.map(r => ({
...r,
id: Number(r.id),
active: r.active === 'true'
}));
Automatic type-guessing is convenient but risky: a ZIP code like 02138 or a phone number can lose leading zeros the instant it's treated as a number.
Common pitfalls
- No header row. If your CSV starts straight into data, the first record gets consumed as column names. Add a header or handle it explicitly.
- Duplicate column names. Two columns named
namecollide, since object keys must be unique — one value wins. - Alternate delimiters. Some exports use semicolons or tabs (TSV). Make sure the delimiter matches your data.
- Trailing empty lines. A blank final line can produce an empty or malformed object — trim it.
- Encoding and BOM. Files exported from Excel sometimes carry a byte-order mark that sneaks into the first header name.
When in doubt, keep values as strings on conversion and cast types in code. It's far easier to add precision deliberately than to recover a leading zero you already lost.
Related tools
To reverse the process, the JSON to CSV tool turns your objects back into spreadsheet rows. Once you have JSON, the JSON Formatter makes it readable, and if you're generating TypeScript models from the data, the JSON to TypeScript tool infers interfaces for you.
Conclusion
Converting CSV to JSON is about two things done right: detecting the header row so your objects have meaningful keys, and parsing quoted fields correctly so embedded commas and newlines don't shatter your rows. Keep values as strings unless you deliberately cast them, mind the delimiter and encoding, and you'll turn any spreadsheet export into structured, code-ready JSON in seconds.