One JSON Value Per Line

A line-oriented JSON convention groups one encoded JSON value per logical line so a reader can process one record at a time.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Draw the boundaries
  2. Handle errors by line
  3. Resume after a complete record boundary
  4. Define line-level error handling and progress

Draw the boundaries

Three objects stored on three lines can be loaded separately and processed without retaining a surrounding array. A pretty-printed object spanning multiple physical lines does not follow the single-line convention. Line iteration supplies those physical records.

Handle errors by line

A malformed second line can be reported with its source number. Whether the reader continues with the third is an application decision. Enumerated iteration keeps that diagnostic location linked to the original file.[1][2]

Resume after a complete record boundary

A line-based exchange can store one complete JSON value per physical line. A newline in a value is escaped within the serialized string, so it does not have to divide records. Input ending after half of an object is incomplete even if earlier lines were valid. See iterating over lines.

Line 1: {"id":4,"text":"first\nsecond"}
Line 2: {"id":9,"text":"ready"}
Last partial line: {"id": → decode error

Define line-level error handling and progress

An importer can stop on the first invalid line or report a rejection and continue if allowed. Record the last durable boundary before retrying. Document encoding, handling of blank lines and whether a final terminator is required.

References

  1. IETF, RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format ↩
  2. Python Software Foundation, io: Core Tools for Working with Streams ↩