← All JSON guides

JSON guide · 3 min read

How to Convert CSV to JSON Correctly

Convert CSV rows to JSON while handling quoted commas, embedded newlines, duplicate headers, empty values and data types deliberately.

By JSON HearthPublished 2026-08-05Updated 2026-08-22

CSV is not the same as splitting every line on commas

A quoted CSV field can contain delimiters, escaped quotes and line breaks. A compliant parser tracks whether it is inside a quoted field before deciding that a comma or newline ends a value.

name,notes
Asha,"Uses commas, quotes, and line breaks"
Mina,"Said ""hello"""

Treat the header row as a contract

Headers normally become JSON property names. Blank, duplicate or inconsistent headers create ambiguity. Normalize them deliberately and report duplicates instead of silently overwriting one column with another.

Remember that CSV starts as text

CSV does not carry a universal type schema. The text 0012 may be an identifier that must preserve leading zeros, while true may be a literal string rather than a Boolean. Automatic type inference is convenient but risky when the destination contract is known.

Define what an empty cell means

An empty CSV cell can become an empty string, null or a missing JSON property. Those states are not interchangeable. Choose the rule from the destination model rather than letting a generic converter invent semantics.

Convert rows and validate the JSON

After conversion, verify row counts, inspect several records and run JSON syntax validation. If the result feeds an API or database, validate it against that destination's schema as a separate step.

CSV:
id,active
0012,true

Possible JSON when id must stay text:
[{"id":"0012","active":true}]

Check delimiter and encoding assumptions

Exports may use semicolons, tabs or locale-specific encodings rather than commas and UTF-8. If an entire row becomes one field or international text is corrupted, inspect the source format before blaming the JSON conversion.

Use a repeatable import checklist

CSV-to-JSON conversion is a data-model conversion, not merely a syntax change. Most subtle bugs come from hidden assumptions about types and missing values.

  • Confirm delimiter, quote rules and encoding.
  • Reject duplicate or blank headers when they are ambiguous.
  • Define type rules for identifiers, numbers, booleans and dates.
  • Define the meaning of empty cells.
  • Verify row counts and representative records.
  • Validate the final JSON against the destination contract.

Common questions

Frequently asked questions

Why not split CSV lines by comma?

A comma inside a quoted field is data, not a separator. Embedded newlines and escaped quotes also require a proper CSV parser.

Should CSV numbers be converted automatically?

Only when the column contract is known. IDs, postal codes and account numbers often need to remain strings.

What should an empty CSV cell become in JSON?

There is no universal answer. Choose empty string, null or missing property according to the destination contract.