CSV to NDJSON: Preserve Leading Zeros and Multiline Cells
Export CSV records as JSON Lines without guessing cell types or breaking quoted delimiters, embedded newlines and identifiers.

A CSV file stores a table but usually provides no reliable type schema. Converting it to newline-delimited JSON is useful for record-oriented processing, yet automatic type guesses can change the data. A customer code such as 001 is not necessarily the number one, and a date-looking string may be a label. Start by choosing a preservation policy, then frame each resulting object as one complete JSON line.
Parse cells before constructing objects
The first row supplies property names, and every following row must contain the same number of cells. A comma inside a quoted cell is not a separator, and a quoted line break is not a new record. Use a parser that handles those cases rather than splitting on commas and newlines. Duplicate header names must be rejected because a JSON object cannot retain two distinct values under the same property name without changing the representation. Empty headers also need an explicit decision rather than an accidental generated name.
Keep source strings until a schema says otherwise
Our converter preserves every cell as a string, including empty cells, leading-zero identifiers, booleans and date-looking values. That policy avoids silently turning a code into a number or applying a locale-specific date interpretation. If your receiving pipeline requires numbers or nulls, apply a separate schema-aware transformation afterward and validate it. This makes the point where a type decision occurs visible and testable. Spreadsheet applications may already have modified the source values before export, so inspect the actual file rather than only its displayed cells.
Keep record framing distinct from embedded newlines
Each output object is serialized without pretty-printing and placed on its own physical line. A line break inside a string becomes a JSON escape sequence, preserving the value without breaking NDJSON framing. A final newline is included for text-processing convenience. The browser converter processes the complete input in memory and bounds the number of data cells; it is not a streaming system. For large production imports, use a streaming parser and record errors with original row positions instead of silently discarding malformed input.
Worked example
CSV:
id,note
001,"hello, team"
NDJSON:
{"id":"001","note":"hello, team"}
Should empty CSV cells become null?
Only if your import contract explicitly says so. An empty string and JSON null carry different meanings; the converter preserves the empty string.


