Six Edge Cases Most CSV-to-JSON Converters Get Wrong
From RFC 4180 quote escapes to type inference corrupting IDs, here are the CSV ↔ JSON conversion pitfalls you only notice once they hit production.
CSV looks like the simplest format on earth: lines of text separated by commas. That illusion is exactly why so many converters - including the one bundled with your favourite spreadsheet - quietly mangle data on the way to JSON. The bugs almost never surface during a quick demo. They surface a month later, when a customer reports that their phone number became a number, their leading zero disappeared, or a row containing a comma in an address field shifted every column to the right.
This article walks through the six conversion edge cases we see go wrong most often, what RFC 4180 actually says about each one, and how to think about them before you ship.
1. Type Inference Will Eat Your IDs
The single most damaging default in any CSV-to-JSON tool is aggressive type inference. The logic looks innocent: "if a field looks like a number, output a number; if it looks like a boolean, output true or false." And then this happens:
id,phone,zip
0731,0014155551234,01075
Run that through a naive converter and you get:
[{ "id": 731, "phone": 14155551234, "zip": 1075 }]
Three columns, three silent corruptions. Leading zeros are gone. The ID 0731 is now 731 and will never look up the same row again. The phone number has been demoted to a 64-bit integer, fine for one US number but wrong the moment a + or a country code shows up. ZIP code 01075 (Belchertown, MA) is now 1075 and will fail validation in any system that expects five digits.
The fix is twofold. First, type inference should be off by default. JSON-from-CSV is supposed to be a faithful representation of the source; coercion is a separate decision. Second, even when inference is on, it should refuse to convert anything that has a leading 0 followed by another digit, anything longer than 15 digits (beyond Number.MAX_SAFE_INTEGER), or anything with leading/trailing whitespace inside the quotes. Our CSV ↔ JSON converter ships with type inference off, and the toggle is paired with an explicit hint about exactly these cases.
2. Quotes Inside Quoted Fields Need Doubling, Not Escaping
RFC 4180 specifies one - and only one - way to embed a " inside a quoted CSV field: double it. So the literal value He said "hi" is encoded as:
quote
"He said ""hi"""
Three confusions show up here. First, plenty of converters expect a backslash escape ("He said \"hi\"") because that is what string literals look like in C, JavaScript, and JSON. CSV does not. A backslash before a quote in a CSV field is just a literal backslash followed by a closing quote - which usually corrupts the rest of the row. Second, the outer quotes only count as field delimiters when they appear at the very start or very end of the field; a " in the middle of an unquoted field is a literal character. Third, an unclosed quote means the entire rest of the file is one giant field, so a single missing " typically destroys every row downstream.
A correct parser is a tiny state machine: it tracks an inQuotes flag, treats "" while in quotes as a literal ", and reports the line number of the first unmatched quote it sees. We wrote the converter's parser as a single-pass loop with exactly that contract, which is why a stray quote on row 47 of a 100,000-row file produces Line 47: unmatched quote instead of dumping the rest of the file into one cell.
3. Newlines Inside Quoted Fields Are Legal
name,bio
"Alice","line 1
line 2"
That is a valid two-row file (header + one data row), not a three-row one. RFC 4180 explicitly allows \n, \r, and \r\n inside quoted fields, and any spreadsheet that has ever exported a multi-line cell relies on this. Naive line-by-line splitters - the input.split("\n") school of CSV parsing - get this wrong every time and turn one row into two malformed ones.
The general rule: never split a CSV by newlines before respecting quote state. The only safe approach is character-by-character (or byte-by-byte) until the parser knows whether each newline is a record terminator or a literal piece of a field. The same principle applies in reverse when stringifying JSON to CSV: any field whose value contains ,, ", \n, or \r must be wrapped in quotes, with internal " doubled. Skip that check and you produce a file that round-trips fine through your own tool but breaks Excel, Numbers, and every other consumer.
4. Trailing Empty Fields and Blank Rows Carry Information
a,b,c
1,,3
1,2,
,,
How many columns does row 2 have? Three: "1", "", "3". Row 3? Also three: "1", "2", "". Row 4? Three empties. A surprisingly common bug is to trim trailing empty fields, which means a converter happily turns row 3 into a two-column object and silently drops the c key:
[{ "a": "1", "b": "", "c": "3" }, { "a": "1", "b": "2" }, { "a": "", "b": "", "c": "" }]
That is wrong. Once a header row declares three columns, every subsequent row has three columns - the trailing empty is data, not whitespace. The same logic applies to a row that is just ,,: it is a record of three empty fields, not an empty row. A defensible parser keeps every field a header promised, padding shorter rows with "" and reporting an error when a row produces more fields than the header declared.
5. Nested Objects Need a Strategy, Not a Guess
JSON is a tree. CSV is a table. There is no spec-blessed way to put {"address": {"city": "NYC", "zip": "10001"}} into a single CSV row, so every converter has to pick a strategy. The two reasonable ones:
- Flatten with dot paths. Each leaf becomes its own column. The example above produces headers
address.city,address.zipand a single data rowNYC,10001. This keeps the data tabular, which is what a downstream pivot table or BI tool wants, but it forces you to decide what to do when arrays appear inside the tree (tags.0,tags.1, ...) and when two paths collide. - Stringify nested values. The whole
{"city":"NYC","zip":"10001"}blob becomes a single cell. The CSV stays narrow, the value is reversible, and the consumer needs a JSON parser to use it. This is the right call when nested objects are heterogeneous between rows.
The wrong move is to silently pick one - usually [object Object] - and call it a day. We expose both strategies on the converter so you make the trade-off explicitly. If you need to inspect or reshape the tree before going to CSV, route the JSON through our JSON Formatter first; it makes it obvious where the nested branches live.
6. NDJSON Is Not JSON
NDJSON (and the equivalent JSONL) is a line-delimited stream of independent JSON values:
{"id":1,"name":"Alice"}
{"id":2,"name":"Bob"}
That file is not valid JSON. Feed it to JSON.parse() and you get a SyntaxError on the second line break. Plenty of "JSON to CSV" tools refuse the input here, even though NDJSON is the dominant format for log pipelines, BigQuery exports, OpenAI fine-tuning datasets, and anything that needs to stream rather than fit-in-memory.
The right approach is to treat NDJSON as a first-class input shape: split on \n, parse each non-empty line as its own JSON value, then run the same array-of-objects → CSV pipeline you would for a regular array. The same shape also makes a great output mode when the consumer is a tool that wants line-delimited records (one row per line, no surrounding [, no commas between rows). Our converter accepts NDJSON as input and offers it as a JSON output mode for exactly that reason.
RFC 4180 in One Paragraph
If you remember nothing else: a CSV field is enclosed in " whenever it contains ,, ", \n, or \r; an interior " is escaped by doubling it; record separators are CRLF (canonically) or LF; and the header row, when present, has the same field count as every data row. That is roughly the entire spec. Our parser implements it exactly, which is why your weird real-world export with mixed CRLF, embedded newlines, and Excel-style doubled quotes survives the round trip into JSON and back. To check a round trip on your own file, compare the original and the re-exported CSV in the diff checker with line endings ignored.
Try It Yourself
Paste any CSV or JSON into the CSV ↔ JSON converter and watch the parser handle these cases live: type inference is off by default, RFC 4180 quoting is built in, NDJSON is a first-class shape, and nested objects come with both flatten and stringify strategies. If your next step is exploring or validating the resulting JSON, pair it with our JSON Schema generator to lock in a shape before it reaches your API.