Skip to content
DevTinyTools

Converting

CSV to JSON: delimiters, types and quotes

CSV stores every cell as text, while JSON distinguishes strings, numbers, booleans and null. A clean CSV-to-JSON conversion does not guess types aggressively; it converts only the values that round-trip byte for byte, and leaves ambiguous text alone.

8 min readTool: CSV to JSON

Delimiters, quotes and embedded newlines

RFC 4180 defines CSV cleanly, yet real exports still trip naive line.split(",") scripts in three places: commas inside quoted fields, doubled quotes (""), and literal line breaks inside a single cell. Adding European semicolon exports and TSV files makes blind splitting even worse.

A proper parser scans character by character, tracks whether it is inside quotes, and counts candidate delimiters (,, ;, \t) only outside quoted text. If a quote is never closed, it should fail loudly with a row number instead of swallowing the rest of the file into one giant string.

Safe round-trip typing: convert only what stays exact

CSV has no types: every cell arrives as text. Naive converters parse anything that matches /^\d+$/ into a number, silently losing trailing zeros, leading zeros and the last digits of 64-bit IDs. The safe rule is simple: convert a cell to a JSON number only if String(Number(cell)) === cell.

Comparison of naive vs round-trip-safe CSV to JSON type inference
CSV cellNaive converterRound-trip ruleWhy
4242 (number)42 (number)String(Number("42")) === "42" — exact round-trip.
19.5019.5 (number)"19.50" (string)Number("19.50") drops the trailing zero, corrupting formatted prices.
021342134 (number)"02134" (string)Leading zero is lost if parsed as a number; ZIP codes break.
90071992547409939007199254740992 (rounded)"9007199254740993" (string)Exceeds Number.MAX_SAFE_INTEGER (2^53 - 1); keeps exact digits as text.
(empty cell)"" (empty string)nullMatches JSON → CSV where null exports as an empty cell.
true / TRUEboth → truetrue → true, TRUE → "TRUE"Lowercase true/false are JSON booleans; uppercase words often mean spreadsheet text.

Unflattening dotted headers into nested objects

Spreadsheets created from JSON often flatten nested objects into dotted headers such as user.name and user.role. Reversing that structure turns each row back into the nested object an API expects:

# CSV input
id,user.name,user.role
1,"A, B",admin

# JSON output (with unflattening enabled)
[
  {
    "id": 1,
    "user": {
      "name": "A, B",
      "role": "admin"
    }
  }
]

You can inspect both directions side by side using the CSV to JSON converter and its companion JSON to CSV converter.

Handling ragged rows and duplicate headers

  • Short or long rows. Hand-edited spreadsheets often drop trailing empty cells or add stray values past the last header. Pad missing cells with null, drop headerless extras, and surface a visible warning rather than failing silently.
  • Duplicate column names. Two columns both named id would collide in a JSON object; renaming the second to id_2 preserves both columns.
  • Empty headers. Trailing commas in a header row create blank column names, which can be labelled column1, column2 instead of producing "" keys.

Frequently asked questions

Why does my European CSV export fail to split columns?
In locales where the comma is the decimal separator, Excel exports CSVs separated by semicolons (;) rather than commas. A parser that counts delimiters outside quoted strings detects whether commas, semicolons or tabs separate the columns automatically.
Why did 19.50 and 02134 stay strings in the JSON output?
Because JSON has no representation for a number with trailing decimal zeros or leading integer zeros. Converting a cell to a number only when String(Number(cell)) equals the exact original text keeps prices, ZIP codes and 19-digit IDs intact.
How do dotted column headers like user.name work?
When header unflattening is enabled, user.name and user.role combine back into {"user":{"name":...,"role":...}}, reversing the flattening used when exporting JSON to CSV.
What happens if two columns have the same header name?
JSON object keys must be unique, or the second column overwrites the first. Renaming duplicates with a numeric suffix (id, id_2) preserves every cell without silent data loss.
Is my CSV uploaded when I convert it?
No. Parsing, delimiter detection and type inference run entirely inside the browser tab via FileReader and JavaScript, so customer exports never leave your machine.