jsonl API reference
JSON Lines (JSONL / NDJSON): newline-delimited JSON, one independent value per line. A thin framing layer over json - each record is a json.Value, so encode / decode compose json.encode / json.decode with a \n split / join, and the file helpers add fs. Pure Jennifer; both binaries.
encode(records) writes one compact JSON value per line (trailing newline); decode(text) parses each non-blank line back into a json.Value, so decode(encode(records)) round-trips. Whole-file readFile / writeFile / appendFile cover the common case; a streaming Reader reads one record at a time for files too large to hold in memory.
Import with import "jsonl.j" as jsonl;. See the jsonl guide for prose and examples.
Contents
| Name | What it is |
|---|---|
appendFile | Encode records and append them to a file (created if missing) - the common JSONL pattern of adding rows to ... |
closeReader | Close a streaming reader. |
closeWriter | Close a streaming writer (closes the underlying file handle). |
decode | Decode JSONL text into records, one json.Value per non-blank line. |
encode | Encode records as JSONL: one compact JSON value per line, each terminated by a newline. |
hasMore | Whether the reader is not yet at end-of-file. |
openReader | Open a JSONL file for streaming. |
readFile | Read and decode a whole JSONL file. |
readRecord | Read and decode the next record, skipping blank lines. |
readValue | Read and decode the next record, skipping blank lines, and return the json.Value directly. |
reader | Wrap an already-open, read-mode fs.File as a streaming reader. |
writeFile | Encode records and write them to a file, replacing any existing content. |
writeValue | Append one record to the writer: the compact JSON encoding followed by a newline. |
writer | Wrap an open write/append-mode fs.File as a streaming JSONL writer. |
Reader | struct: A line-at-a-time reader over an open file, for JSONL too large to hold in memory. |
Record | struct: One streaming read: a decoded record, or done set once the stream is exhausted. |
Writer | struct: A streaming JSONL writer over an open fs.File. |
Functions
appendFile
jsonl.appendFile(path as string, records as list of json.Value)Encode records and append them to a file (created if missing) - the common JSONL pattern of adding rows to a growing log.
path- the file path
records- the records to append
closeReader
jsonl.closeReader(reader as Reader)Close a streaming reader.
reader- the reader
closeWriter
jsonl.closeWriter(writer as Writer)Close a streaming writer (closes the underlying file handle).
writer- the writer
decode
jsonl.decode(text as string)Decode JSONL text into records, one json.Value per non-blank line. Blank lines and lines of JSON whitespace only (space, tab, CR) are skipped, and a trailing \r (CRLF input) is trimmed, so decode(encode(records)) round-trips. Other whitespace, such as U+00A0, is not JSON's and makes the line invalid. A line that is not valid JSON throws json's error, its message prefixed with jsonl: line N:.
text- the JSONL text
- Returns
list of json.Value - the parsed records
encode
jsonl.encode(records as list of json.Value)Encode records as JSONL: one compact JSON value per line, each terminated by a newline. An empty list yields the empty string.
records- the records to encode
- Returns
string - the JSONL text
hasMore
jsonl.hasMore(reader as Reader)Whether the reader is not yet at end-of-file. This is a coarse check: it can report true when only trailing blank lines remain, which carry no record. Prefer looping on readRecord(...).done - the reliable end signal.
reader- the reader
- Returns
bool - true if the file still has unread bytes
openReader
jsonl.openReader(path as string)Open a JSONL file for streaming.
path- the file path
- Returns
Reader - the reader
readFile
jsonl.readFile(path as string)Read and decode a whole JSONL file.
path- the file path
- Returns
list of json.Value - the records
readRecord
jsonl.readRecord(reader as Reader)Read and decode the next record, skipping blank lines. Returns a Record: {value, done: false} for a record, or {done: true} once the stream is exhausted (including when only trailing blank lines remained). Loop until done rather than guarding with hasMore, which cannot see trailing blanks. Blank lines are those decode skips; a line that is not valid JSON throws json's error, its message prefixed with jsonl: line N: as in decode.
reader- the reader
- Returns
Record - the next record, or a
donemarker at end
readValue
jsonl.readValue(reader as Reader)Read and decode the next record, skipping blank lines, and return the json.Value directly. At end-of-stream this returns a JSON null; guard the loop with not fs.eof(...) (JSONL written by writeValue has one value per line and no trailing blanks) or use readRecord when you need the explicit done flag to distinguish end-of-stream from a genuine null record.
reader- the reader
- Returns
json.Value - the next record, or a JSON
nullat end
reader
jsonl.reader(file as fs.File)Wrap an already-open, read-mode fs.File as a streaming reader. Use this when the caller owns the file handle (openReader opens the path for you). The file is a handle, so successive readValue calls advance the same stream.
file- an open read-mode file handle
- Returns
Reader - the reader
writeFile
jsonl.writeFile(path as string, records as list of json.Value)Encode records and write them to a file, replacing any existing content.
path- the file path
records- the records to write
writeValue
jsonl.writeValue(writer as Writer, value as json.Value)Append one record to the writer: the compact JSON encoding followed by a newline.
writer- the writer
value- the record to append
writer
jsonl.writer(file as fs.File)Wrap an open write/append-mode fs.File as a streaming JSONL writer.
file- an open write- or append-mode file handle
- Returns
Writer - the writer
Structs
Reader
A line-at-a-time reader over an open file, for JSONL too large to hold in memory. Build with openReader; the wrapped fs.File shares its read position across value copies (a handle), so successive readRecord calls advance the same stream.
def struct Reader {
file as fs.File,
lines as channel of int
};file- the underlying open file handle
lines- the lines read so far, for error messages (internal, shared by every copy like the file)
Record
One streaming read: a decoded record, or done set once the stream is exhausted. done is the reliable end signal - a raw not eof check is not, because trailing blank lines leave the file un-exhausted yet carry no record.
def struct Record {
value as json.Value,
done as bool
};value- the decoded record (undefined when
done) done- true once no further record remains
Writer
A streaming JSONL writer over an open fs.File. Each writeValue call appends one compact JSON value terminated by a newline, so a growing log is written without building the whole text in memory.
def struct Writer {
file as fs.File
};file- the underlying open write/append-mode file handle