← Guides

CSV and JSON: Quoting Rules, Type Inference and Header Traps

Two shapes of tabular data

CSV is a line-oriented text format: one record per line, fields separated by a delimiter, no types and no nesting. JSON is a tree: objects with named keys, arrays, typed scalars and no column concept at all. Converting between them is not symmetric — a well-formed JSON document can be flattened into rows, but a CSV file only becomes JSON once you decide what the first row means.

The quoting rules that trip up simple parsers

RFC 4180 says a field containing the delimiter, a double quote or a line break must be wrapped in double quotes, and an embedded quote is written twice. That means this is a single record with three fields:

"Smith, J.",42,"said ""hello""

Parsing it with line.split(',') produces four columns and silently corrupts the data. Any parser that does not run a character-by-character state machine will break on addresses, product names with commas, and free-text comment columns. Excel and LibreOffice both write valid quoted CSV, so the exporter is rarely the problem; the naive parser is.

Delimiters are not always commas

In locales where the decimal separator is a comma, exports often use a semicolon instead, and tab-separated files remain common for log data and database dumps. Detect the delimiter by counting candidates outside quotes on the first few lines, or let the user choose it — guessing from a single line is unreliable.

Type inference is a decision, not a detail

CSV has no types: every cell is text. When converting to JSON you must choose whether "42" becomes the number 42 or stays a string. Converting to numbers is convenient for arithmetic but silently destroys values that only look numeric:

  • Leading zeros — 007, postcodes, sort codes and phone numbers lose information once they become 7.
  • Long integers — account numbers above 253 lose precision in JavaScript.
  • Empty cells — becoming null is usually right, but an empty string and a missing value are not the same thing.
  • Boolean-looking text — true and false may be real booleans, or may be labels the consumer expects as strings.

When the destination is a strict schema, convert everything to strings first and let the application cast the fields it knows about. Guessing types at the edge is where most silent data loss happens.

Headers, duplicates and empty names

JSON object keys must be unique; CSV headers are not guaranteed to be. Two columns named price collapse into one key and the second silently wins. A robust converter renames duplicates (price, price_2) or refuses to convert. Blank header cells are the same problem in disguise and need generated names such as column.

Encoding and line endings

Always save CSV as UTF-8. A file re-exported from a legacy spreadsheet may be Windows-1252, which turns accented characters and CJK text into mojibake that no downstream conversion can undo. Line endings also vary — CRLF on Windows, LF elsewhere — and a parser should accept both rather than leaving a stray \r at the end of every value.

Nested data does not survive the round trip

JSON objects and arrays have no CSV equivalent. Common workarounds are joining array values with a separator, stringifying nested objects into one cell, or producing a second table with a foreign key. None is lossless by default, so expect a second pass when importing. Going the other direction, a JSON array of objects maps cleanly to rows, but a single object becomes a one-row table and a bare scalar becomes a single cell.

Which format for which job

Use CSV for spreadsheets, bulk imports, log exports and anything a human will open in Excel. Use JSON for APIs, configuration, nested structures and anything consumed by code that expects types. Convert when handing data between the two worlds, not as a storage strategy.

Convert in either direction with our CSV ⇄ JSON converter. It follows the RFC 4180 quoting rules, supports comma, tab, semicolon and pipe delimiters, optionally treats the first row as headers, and renames duplicate columns instead of dropping them.

Try it yourself: open the CSV ⇄ JSON Converter →