CSV stores data as rows of comma-separated values, while JSON stores the same data as an array of objects. Converting one to the other is mostly mechanical, but the details — headers, quoting and types — decide whether the result is clean or broken.
The mapping: headers become keys
Each CSV column header becomes a JSON property name, and each data row becomes one object in an array. Take this input:
name,age,city
Ada,36,London
Linus,54,Helsinki
The converter produces:
[
{ "name": "Ada", "age": "36", "city": "London" },
{ "name": "Linus", "age": "54", "city": "Helsinki" }
]
Every value is a string by default, because CSV has no concept of types. Turning "age": "36" into the number 36 is a deliberate extra step.
Step by step
- Check the delimiter. Most files use commas, but some use semicolons, tabs or pipes. Detect it before splitting.
- Read the header row. Trim extra spaces so
first namebecomes the keyfirst name, notfirst name. - Split each row into fields. Respect quotes: a field wrapped in double quotes may itself contain commas.
- Build an object per row. Zip the header names with the row values.
- Collect the objects into an array and serialise to JSON.
Quoted fields and commas
The trickiest part of CSV is that a value can contain the delimiter. This row:
name,address
Smith,"12 Oak Street, Springfield"
has two fields, not three, because the comma sits inside quotes. A naive split on commas would create a phantom column. The rule inside quotes: commas are literal, and a doubled quote ("") represents one real quote character.
Missing values and ragged rows
Real files are rarely tidy. A row may have fewer fields than the header (a missing value) or more (an unescaped delimiter). Decide up front how to handle a short row — usually by filling the gap with null or an empty string — so your JSON objects keep a consistent shape. Consistency matters because most consumers expect every object to have the same keys.
Common pitfalls
| Issue | What to do |
|---|---|
| Numbers arrive as strings | Convert the fields you know are numeric. |
| Dates look like text | Choose a format (often ISO 8601) and parse it. |
| Duplicate headers | Rename them so keys stay unique. |
| Line endings differ | Normalise
and
before parsing. |
Choosing the right tool
For a one-off job, a browser converter is the fastest route. For a repeatable pipeline, use a library so the parsing rules stay consistent. Either way, always check a few rows by eye after converting; CSV's loose rules mean bad data rarely announces itself.
Paste your CSV into our CSV to JSON converter to get a structured array with quoted fields handled automatically, and read JSON vs CSV to decide which format to keep as your source of truth. If your data contains URL-encoded values, the URL encoding guide explains how to read them.