Convert CSV to JSON with Header Row

Convert CSV with a header row into a JSON array, one object per line, with the first row supplying the keys. Quoted commas and line breaks are handled.

Loading tool…

How It Works

To convert CSV to JSON with a header row, you take the first line as the list of property names and turn every following line into one object, matching values to names by column position. This page does exactly that, and the details below are the ones that decide whether the output drops data.

Parsing first, naming second. The file is parsed as a matrix of cells, following RFC 4180: a quoted field may contain the delimiter, a line break or a doubled quote, and CRLF from Windows is accepted alongside LF. Blank lines are skipped so a trailing newline does not create an empty record. Only after the matrix exists is the header row zipped onto each body row, which is what keeps an unusual column name such as __proto__ as ordinary data instead of letting it collide with object internals.

Values stay strings. Nothing is coerced to a number or a boolean. That is deliberate: a CSV column holding 0042 or a 19-digit account number would lose its leading zero or its precision if it were converted. If you need typed JSON, convert the values after import.

Ragged rows and repeats. A row with fewer cells than the header gets null for the missing columns rather than being padded with empty strings or dropped. A header that repeats a name cannot produce two identical keys, so the second occurrence is suffixed: a,a,b becomes a, a_2, b, and every value survives.

Without a header row. Switching the header option off treats every line as data and names the columns field1, field2 and so on, which is the right choice for a file whose first line is already a record.

The output is a JSON array with two-space indentation, ready to paste into a fixture, a seed script or an API test.

How to convert a header-row CSV in four steps

  1. Paste the file with its header line first; if the data starts on line one, switch the header option off instead.
  2. Check the delimiter matches the file — comma, semicolon or tab — because a wrong delimiter merges every column into one.
  3. Scan the first object: a null value marks a short row, and an _2 key marks a repeated header name.
  4. Cast numeric and boolean strings in your own code after import, since the converter deliberately returns everything as strings.

CSV input and the JSON it produces

CSV pastedJSON producedHandling rule
name,age then Alice,30[{"name":"Alice","age":"30"}]The header names the keys; every value stays a string
"Smith, John",30 under name,age[{"name":"Smith, John","age":"30"}]A quoted delimiter does not split the field
a,a then 1,2[{"a":"1","a_2":"2"}]A repeated header is suffixed instead of overwriting
Alice,30 under name,age[{"name":"Alice","age":null}]A short row gets null, which flags the missing cell
name,age with no data lines[]A header-only file is an empty array, not an error

Examples

Input

name,age
Alice,30
Bob,25

Output

[
  {
    "name": "Alice",
    "age": "30"
  },
  {
    "name": "Bob",
    "age": "25"
  }
]

The header line names the keys, the two data lines become objects, and 30 stays a string.

Input

a,a
1,2

Output

[
  {
    "a": "1",
    "a_2": "2"
  }
]

A repeated header is suffixed instead of overwritten, so the second column is not lost.

Input

name,age
Alice

Output

[
  {
    "name": "Alice",
    "age": null
  }
]

Boundary case: a ragged row. The missing cell becomes null, which shows the file is short a column.

Input

name,age

Output

[]

Boundary case: a header with no data lines converts to an empty array rather than an error.

Frequently Asked Questions

What does with header row actually mean?
It means the first CSV line supplies the property names and is not itself data. Each later line becomes one object whose keys come from that first line, matched by position. If the first line were treated as data too, your JSON would contain a record describing the column titles.
Why are the numbers in my JSON quoted?
Because CSV carries no type information; every cell is text. Converting automatically would damage identifiers: 0042 would become 42 and a long account number would lose digits beyond the safe integer range. Cast the fields you need after conversion, in your own code, where you know which columns are numeric.
What happens to a row with too few columns?
The missing cells become null, so the object keeps every key from the header and the shortfall is visible. A row with extra cells keeps only the values that match a header name. Either way you get a hint that the file is malformed rather than silently truncated output.
What if two columns have the same name?
The first keeps the name and later ones are suffixed _2, _3 and so on, so a,a,b produces the keys a, a_2 and b. Renaming in the source file is still the better fix, because a suffixed key usually means the export was assembled incorrectly upstream.

Limitations & Accuracy

Every value is emitted as a string, because CSV has no type information; casting numbers and booleans is left to your code. A header-only file returns an empty array, and a row with fewer cells than the header gets null for the missing columns. Repeated header names are suffixed _2 and _3 rather than merged. Only comma, semicolon and tab delimiters are offered, and the direction selector stays editable, so the tool can also convert JSON back to CSV.

Related Tools