JSONL: a practical guide to JSON Lines
By Flavio
Tagged: CLI
Learn how JSONL stores one JSON value per line, how to write, stream, and query it, and why I use it as Work Tracebook's event store.
JSONL is one of those formats that looks almost too simple. It’s just JSON, except every line is its own separate JSON value.
I use it when I want to keep adding structured records to a file without loading and rewriting the whole thing. Logs, events, exports, and communication between small programs are all good uses for it.
Let’s see how it works, then I’ll show you how I use it in Work Tracebook, an app I use to log data about what I did with AI agents. It’s available now.
JSONL ↔ JSON Converter
Convert JSONL (JSON Lines, NDJSON) to a JSON array and back, with per-line validation, error positions, and key counts.
What is JSONL?
JSONL, short for JSON Lines, is a text format that stores one complete JSON value per line. It’s also called newline-delimited JSON or NDJSON. A JSONL file usually has the .jsonl extension, and because each line is a separate record, you can append new records without rewriting the file and read them one line at a time.
What JSONL looks like
Here’s a small events.jsonl file:
{"timestamp":"2026-08-27T08:00:00Z","kind":"task.started","title":"Add search"}
{"timestamp":"2026-08-27T08:18:00Z","kind":"task.reported","summary":"Added the search form"}
{"timestamp":"2026-08-27T08:31:00Z","kind":"task.archived","summary":"Finished search and verified the build"}
This file contains three JSON objects. Each one is complete on its own, and the newline after it marks the end of the record.
The JSON Lines format has three main rules:
- the file uses UTF-8
- every line is a valid JSON value
- records are separated by a newline character
Objects are the most common values, but a line can hold any valid JSON value: an array, string, number, boolean, or null. A blank line isn’t a valid JSON value, so blank lines aren’t allowed.
Files usually use the .jsonl extension, though you’ll also see .ndjson, especially in systems that follow the NDJSON specification.
JSONL compared to a JSON array
We could store the same records in a regular JSON array:
[
{"timestamp":"2026-08-27T08:00:00Z","kind":"task.started","title":"Add search"},
{"timestamp":"2026-08-27T08:18:00Z","kind":"task.reported","summary":"Added the search form"},
{"timestamp":"2026-08-27T08:31:00Z","kind":"task.archived","summary":"Finished search and verified the build"}
]
That’s one JSON value: an array containing three objects. Adding another record is inconvenient because you have to insert a comma and a new object before the closing bracket. Most programs end up reading the whole array, changing it in memory, and rewriting the file.
With JSONL, you just append one line:
{"timestamp":"2026-08-27T09:05:00Z","kind":"task.started","title":"Improve the empty state"}
Existing records don’t change.
This also makes JSONL useful for streams, since a reader can process the first record before the writer even produces the last one.
The tradeoff is that the complete file isn’t one valid JSON document. You can’t pass the whole file to JSON.parse(), so you have to parse it one line at a time.
If JSON syntax is new to you, start with my introduction to JSON.
Write a JSONL file with Node.js
Let’s append an event using Node.js:
import { appendFile } from 'node:fs/promises'
const event = {
timestamp: new Date().toISOString(),
kind: 'task.started',
title: 'Add dark mode'
}
const line = `${JSON.stringify(event)}\n`
await appendFile('events.jsonl', line, 'utf8')
JSON.stringify() turns the object into compact JSON without line breaks, then we add \n and append the result. Always include that trailing newline so the next append is unambiguous and you can concatenate JSONL files safely.
Don’t pretty-print objects with JSON.stringify(event, null, 2). That spreads a single value across multiple physical lines and breaks the format. If a string field contains a line break, JSON encodes it as \n inside the string, so the record still stays on one physical line.
Read a small JSONL file
For a small file, you can read the text and parse each line:
import { readFile } from 'node:fs/promises'
const text = await readFile('events.jsonl', 'utf8')
const lines = text.split('\n')
if (lines.at(-1) === '') {
lines.pop()
}
const events = lines.map((line, index) => {
if (line === '') {
throw new Error(`Blank line at ${index + 1}`)
}
return JSON.parse(line)
})
Removing the last empty string handles the trailing newline while still rejecting empty lines in the middle of the file.
JSON.parse() throws if a record is malformed. In a real importer, catch that error and report the line number so it’s easy to find and fix the broken line.
Stream a large JSONL file
Reading the whole file at once is fine when it’s small. For a large log or export, process it one line at a time:
import { createReadStream } from 'node:fs'
import { createInterface } from 'node:readline'
const lines = createInterface({
input: createReadStream('events.jsonl'),
crlfDelay: Infinity
})
for await (const line of lines) {
if (line === '') {
throw new Error('Found a blank line')
}
const event = JSON.parse(line)
console.log(event.kind)
}
This keeps memory usage nearly constant, so the file can hold thousands or millions of records without turning into one huge JavaScript array.
crlfDelay: Infinity makes the reader handle both Unix \n and Windows \r\n line endings.
Inspect JSONL from the command line
JSONL works well with standard command-line tools.
Count the records:
wc -l events.jsonl
Show the three most recent events:
tail -n 3 events.jsonl
Use jq to select archived tasks:
jq -c 'select(.kind == "task.archived")' events.jsonl
jq reads each JSON value and runs the filter on it, and -c keeps each result on a single line.
Use --slurp when you want one array containing all records:
jq --slurp 'map(select(.kind == "task.archived"))' events.jsonl
Be careful with --slurp on large files. It loads the entire input into memory, giving up one of JSONL’s main advantages.
Appending is not the same as concurrency safety
The Node.js example above works fine when a single process owns the file, but it isn’t enough when multiple processes write at the same time. JSONL only defines the record format; it doesn’t give you locks, transactions, validation, or crash recovery.
Two writers can read the same state and append conflicting records, or a process can crash halfway through writing a line.
If you have multiple writers, use a single dedicated writer, a cross-process lock, or a storage system that gives you the guarantees you need.
How I use JSONL in Work Tracebook
Work Tracebook keeps a private, local history of work completed by coding agents. You can download the app.
An agent starts a task, reports meaningful milestones, then archives the task when the work is finished:
factorylog start \
--title "Add account settings" \
--summary "Started the settings screen" \
--source codex
The command returns JSON containing a generated taskID, which the agent keeps for later updates:
factorylog report \
--task-id task_123 \
--summary "Added validation and tests"
When the task is finished, it archives it:
factorylog archive \
--task-id task_123 \
--summary "Finished and verified account settings"
Each command appends one event to this file:
~/Library/Application Support/Work Tracebook/events.jsonl
A real record looks like this:
{"id":"9d61dbef-f829-4437-aaa1-371015450427","kind":"task.started","project":{"name":"flaviocopes.com","path":"/Users/flavio/Projects/flaviocopes.com"},"schemaVersion":1,"source":{"tool":"codex"},"summary":"Drafting a JSONL tutorial grounded in Work Tracebook's real event store","taskID":"task_f4380f90ddd0488682823b8299376adc","taskTitle":"Write JSONL tutorial","timestamp":"2026-08-27T10:42:53Z"}
Every event is self-contained, repeating the project, task title, source, and schema version instead of depending on another file or database row.
Events with the same taskID form one task history:
task.started
task.reported
task.reported
task.archived
The native macOS app watches the JSONL file, groups events by taskID, derives whether each task is Doing or Done, and turns the reports into a dashboard and daily project chronicle. You can see how that daily view works in Keep a timeline of what your coding agents did today.
Work Tracebook only stores short reports written by the agent. It doesn’t collect source code, diffs, secrets, or terminal output.
Why I chose JSONL for Work Tracebook
The format fits the product. A coding agent produces a sequence of events, and JSONL lets the CLI append each one immediately without rewriting earlier history.
The file also stays useful without the app. I can open it in a text editor, inspect it with tail, filter it with jq, copy it, or write a different importer later. I didn’t need a database server or a custom binary format when one local UTF-8 file of ordinary JSON records can be the source of truth.
That said, I didn’t treat appendFile() as a complete storage design. Several coding agents can report at the same time, so Work Tracebook uses a sibling lock file, and both validation and appending happen while holding that cross-process lock. The writer opens the data file in append mode, writes one complete newline-terminated record, and calls fsync() before releasing the lock.
Readers handle failure at the record level. If the last write was interrupted and the file ends with an incomplete line, Work Tracebook ignores that final fragment while keeping earlier events readable. If a complete non-empty line has malformed JSON or an unsupported schema version, the app reports the line number, keeps the rest of the readable records available, and blocks new writes until the broken history is fixed.
Each event also includes schemaVersion: 1 so the format can evolve deliberately instead of guessing what an old record means.
That’s what I like most about JSONL: the format itself stays tiny, and your app adds only the guarantees it needs.
When JSONL is a good fit
I reach for JSONL when records are naturally sequential and mostly immutable:
- application and audit logs
- event histories
- streaming API responses
- machine learning datasets
- large imports and exports
- messages passed through Unix pipes
I wouldn’t use a plain JSONL file when I need frequent updates to old records, indexes across many fields, relational queries, or transactions across several records. At that point, I use SQLite or another database.
JSONL sits between unstructured text and a database. It’s still a plain file, but every line has a structure that programs already know how to parse.
Want me to talk about your product? You can sponsor this site.
Related posts about cli: