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.
| CSV cell | Naive converter | Round-trip rule | Why |
|---|---|---|---|
| 42 | 42 (number) | 42 (number) | String(Number("42")) === "42" — exact round-trip. |
| 19.50 | 19.5 (number) | "19.50" (string) | Number("19.50") drops the trailing zero, corrupting formatted prices. |
| 02134 | 2134 (number) | "02134" (string) | Leading zero is lost if parsed as a number; ZIP codes break. |
| 9007199254740993 | 9007199254740992 (rounded) | "9007199254740993" (string) | Exceeds Number.MAX_SAFE_INTEGER (2^53 - 1); keeps exact digits as text. |
| (empty cell) | "" (empty string) | null | Matches JSON → CSV where null exports as an empty cell. |
| true / TRUE | both → true | true → 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
idwould collide in a JSON object; renaming the second toid_2preserves both columns. - Empty headers. Trailing commas in a header row create blank column names, which can be labelled
column1,column2instead of producing""keys.