Skip to main content
CuteJSON
↑↓ Navigate Enter Select Esc Close

How to convert JSON to NDJSON for logs and streams

Published 2 min read By Akash Kinkar Pandey

NDJSON is one JSON value per line, and that is the entire format. It exists because a reader can handle one line and forget it, which is what makes a log file or a large export streamable when a single enormous array is not: nothing has to hold the whole document in memory at once.

Using the JSON to NDJSON converter

Choose a direction and press Convert. Going to NDJSON needs an array, since that is the JSON shape that is a sequence of values; any element type is allowed on a line, not only objects.

[{"id": 12345678901234567890}, "plain string", 42, null]

becomes:

{"id":12345678901234567890}
"plain string"
42
null

Each element gets its own line, minified, and the twenty digit id keeps every digit because both directions go through the token stream rather than JSON.parse.

Blank lines

A completely blank line is skipped rather than treated as an error. Almost every real NDJSON file ends with a trailing newline, which strictly means a blank last line, so failing on one would reject nearly every file anyone actually has. The tool reports how many it skipped alongside how many records it wrote, since a file that turned out to be half empty is worth knowing about.

Reading back, and what a broken line tells you

Converting NDJSON to JSON validates every line on its own and reports a failure by the line number your file actually has. Given

{"a":1}
{"a":2}
{"a":}
{"a":4}

the failure is reported at line 3, not at line 1 or at the end of the file, so you can go straight to the broken record in a thousand line file without counting.

One value per line, strictly

Two JSON values on a single line is not NDJSON, even though a lenient parser might accept it by accident. This tool rejects it, because accepting it quietly would mean a downstream reader built to the real specification disagrees with what this tool wrote. If you need each record readably indented, use the JSON formatter on the result after converting back to a regular array; NDJSON lines stay unindented on purpose, since a line break inside a value would end the record.

Back to all posts