CheckMySchema

Structured data explained

Is JSONL Valid JSON? Find a Missing Newline Between Two Joined Files

Check each JSON Lines record by itself. If two valid files break after joining, inspect the line at their boundary.

    Three blank index cards are staggered on a pale desk.
    Treat each card as one record to keep separate.
    CheckMySchemaCheckMySchema

    At a glance

    Each nonempty JSON Lines record must be a valid JSON value. Two adjacent objects on one physical line fail the line parser. Check the separator between source files.

    Review when to choose JSON Lines

    A JSON Lines file contains one valid JSON value on each physical line. A multi-record file is normally different from one JSON document. If each of your files parses separately but they fail after joining, look at the boundary between the two inputs first.

    Check the reader and the format it expects

    Confirm whether the destination asks for one JSON array or a stream of JSON Lines. The two may contain the same records but need different serialization. For this check, assume the reader expects JSON Lines and counts one value per line.

    Take a minimal sample of the last record in the first file and the first record in the second. Work with copies. In the example the data is fictional: the first file ends immediately after the object with id 1, and the second starts with the object with id 2.

    For the definition of one JSON document, see RFC 8259: The JavaScript Object Notation Data Interchange Format.

    Sources: JSON Lines · IETF

    Inspect the physical boundary between files

    The first file may parse with no final line terminator. If you append the bytes from the second file immediately, the next opening brace lands beside the first closing brace on the same line. That gives the parser two adjacent values when it expects one. Modern editors can display line endings or show whether the cursor has reached a new line before the file ends.

    Compare the two originals with the reported line in the output. Inspecting the boundary lets you distinguish a missing separator from an already malformed object inside one source. Reproduce the issue with two non-sensitive sample objects before applying a fix to the real data.

    Example file A ends with id 1 and file B begins with id 2.
    Inspect the end of A before concatenating the files.

    For the format’s line termination guidance, see JSON Lines Format Requirements.

    Sources: JSON Lines

    Preserve exactly one separator between records

    When joining two nonempty files that follow the format, preserve the existing separator or add one when the first file lacks a terminator. If you add two newlines between the records, the extra blank line is invalid. Preserve the line boundaries when normalizing CRLF or LF and keep the original files until you have verified the result.

    For large files, read the sources and write to a new destination while keeping track of the boundary. Avoid silently skipping lines that fail. Log which source and line failed without exposing private record contents.

    The joined examples show two objects next to each other and two separated by a single LF.
    LF represents the physical separator, not the letters L and F.
    Blank tabbed index cards stand in a small file.
    Keep the record boundary when you combine two sources.

    One boundary, two outcomes

    Raw concatenation

    The bytes end up as two objects adjacent on one line. That line does not parse.

    A newline at the join

    The parser sees an individual value on each line and retains both records.

    For a streamed line reader example, see Readline API: Read File Line by Line.

    Sources: JSON Lines · Node.js

    Check record count and schema separately

    Read the resulting file one physical line at a time. The example must have two records, with ids 1 and 2 in order. Once both values parse, validate them against the downstream contract if it needs specific fields. An acceptable line boundary does not establish that every record has the expected keys or types.

    Observed results from a two-record local fixture.
    InputPhysical recordsResult
    First file without tail LF1Valid single record
    Raw A + B1One invalid line with two objects
    A + LF + B2Both records parse
    A + LF + LF + B3Blank line fails
    A record checklist checks physical lines, literal ids and schema in order.
    Verify count and meaning after the file parses.

    Verify the repaired output

    • Retain both original files.
    • Inspect the boundary line.
    • Insert a separator only when needed.
    • Confirm the count and record schema.

    For the related task, see Find the reported parse error.

    Sources: IETF · Node.js

    Frequently asked questions

    Is a final newline required?

    The JSON Lines format recommends a final terminator. A single file may parse without it, but the boundary matters when joining files.

    Does a trailing newline add a blank record?

    A single end-of-line marker terminates the last value. An extra empty line between values has no valid JSON value.

    Can I just wrap the joined file in brackets?

    A JSON array also needs commas between its values. Use a JSON serializer when converting to an array.

    Why can clean JSON Lines still fail schema validation?

    Syntax only confirms the record is a JSON value. Your downstream schema can impose field, type or object requirements.

    CheckMySchemaCheckMySchema

    Decide whether to use JSON Lines

    Start with what your receiving tool accepts.

    Review when to choose JSON Lines

    Continue with a related guide

    References