Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool

Handling JSON Lines Format in Specialized Formatters

JSON Lines is not a single pretty-printed JSON document. It is a sequence of separate JSON values written one per line, usually for logs, exports, and streaming pipelines. That is why a normal JSON formatter often fails on line 2, while a formatter with JSONL or NDJSON support can validate and normalize the file correctly.

For search users landing here directly, the key idea is simple: a specialized formatter should treat each physical line as its own JSON payload, report errors by line number, and avoid reformatting records into multi-line blocks unless it is explicitly converting the file into standard JSON first.

JSON Lines, JSONL, and NDJSON

In practice, people often use JSON Lines, JSONL, and NDJSON interchangeably. They all describe line-delimited JSON data, but the naming conventions you see in tools and APIs can differ.

Current conventions worth knowing

  • JSON Lines documentation uses the .jsonl extension and requires UTF-8 with no BOM.
  • JSON Lines allows any valid JSON value on a line, not only objects.
  • The NDJSON spec recommends the .ndjson extension and the media type application/x-ndjson.
  • You may also encounter application/jsonl, but it is not broadly standardized.

That means a capable formatter should not care whether the file is called events.jsonl or events.ndjson. It should care about whether the content actually follows the one-value-per-line rule.

What Makes a JSON Lines File Valid

The rules are stricter than many articles suggest. A valid JSON Lines or NDJSON file is not just “lots of JSON objects separated by Enter.”

Core rules

  • Use UTF-8 encoding.
  • Do not include a byte order mark (BOM).
  • Each line must be one complete JSON value.
  • A line can hold an object, array, string, number, boolean, or null.
  • Blank lines are not valid JSON values unless a parser explicitly chooses to ignore them.
  • The delimiter is a newline. Parsers commonly accept both \n and \r\n.

Valid JSON Lines example

{"id":1,"name":"Apple"}
{"id":2,"name":"Banana"}
null
[1,2,3]

Objects are the most common case, but they are not the only allowed values.

Invalid JSON Lines example

{
  "id": 1,
  "name": "Apple"
}
{"id":2,"name":"Banana"}

The first record is valid JSON, but it spans multiple physical lines. That makes the file invalid as JSON Lines or NDJSON.

Why Standard JSON Formatters Fail

A standard formatter expects one JSON document from the first byte to the last. JSON Lines gives it multiple top-level JSON values back to back, so the parser usually accepts the first line and then throws an “extra data” or “unexpected token” style error when it encounters the second line.

Standard JSON

[
  {"id":1,"name":"Apple"},
  {"id":2,"name":"Banana"}
]

One root array. A normal formatter can parse and indent it.

JSON Lines

{"id":1,"name":"Apple"}
{"id":2,"name":"Banana"}

Two valid JSON values, but not one valid JSON document. A JSONL-aware formatter must switch to line mode.

What a Specialized Formatter Should Actually Do

The useful behavior is not “pretend this is normal JSON.” It is to parse, validate, and rewrite records safely without destroying the line-delimited structure.

  • Split the input on newlines and inspect each non-empty line independently.
  • Parse each line as a complete JSON value.
  • Report exact line numbers for failures instead of stopping with a generic document-level error.
  • Normalize whitespace inside each record and usually write it back as one compact line.
  • Preserve record order so log streams and exports remain meaningful.
  • Offer a separate “convert to array” view when a human wants classic multi-line pretty-printing.

That last point matters. If a formatter expands one record into several physical lines and saves it as-is, the result is no longer valid JSON Lines. Pretty display mode is fine in an interface, but the exported file still needs one complete JSON value per line.

Best Formatting Modes for JSONL and NDJSON

  • Validate in place:

    Keep the file as JSON Lines, flag bad records, and rewrite each valid line in compact canonical form.

  • Inspect as an array:

    Wrap the records in a temporary array for reading, searching, or visual diffing, then export back to JSONL when finished.

  • Stream large files:

    Process record by record instead of loading a huge array into memory. This is one of the main reasons JSON Lines exists in the first place.

Common Problems and How to Fix Them

  • Error after the first line:

    You are probably using a normal JSON parser instead of a line-aware formatter.

  • Blank line in the middle of the file:

    Some tools reject it, while others ignore it only when configured to do so. Remove blank lines if you need predictable cross-tool behavior.

  • Pretty-printed object pasted into a JSONL file:

    Collapse it back to one line before saving. Multi-line records break the delimiter model.

  • Encoding issues or strange first-character errors:

    Check for UTF-8 encoding and remove any BOM at the start of the file.

  • Confusion around newline characters inside strings:

    Escaped sequences like \n inside a JSON string are fine. Literal line breaks inside a record are not.

When to Keep JSON Lines vs Convert to Standard JSON

Keep the data as JSON Lines when you are handling logs, event streams, append-only datasets, or very large exports that benefit from record-by-record processing. Convert it to a standard JSON array when you need human-friendly pretty-printing, schema inspection, or a downstream tool that only accepts one root document.

Practical rule of thumb

If the file needs to stay streamable, preserve one value per line. If the file needs to be easy for humans to browse and edit, convert it to a standard JSON array first, then convert it back when exporting.

What to Look for in a Formatter

A good JSONL formatter should explicitly mention JSON Lines, JSONL, or NDJSON support. It should validate each line independently, tell you which line failed, accept .jsonl and .ndjson inputs, and avoid saving multi-line records unless it is deliberately converting the data into standard JSON.

Conclusion

Handling JSON Lines correctly is mostly about respecting the delimiter model. Each line is its own JSON value, the file is usually UTF-8, blank lines are risky, and classic whole-document pretty-printing is the wrong output mode unless you first convert to ordinary JSON. Specialized formatters succeed because they understand those rules instead of forcing line-delimited data through a normal JSON parser.

Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool