# JSON and JSONL

`--format json` prints one document when the command ends. `--format jsonl` prints one record per line while it runs. Both are for programs, so neither has color, glyphs, prompts or a budget.

## The json document

A program that runs the golden `acme check --json` gets [`json.stdout.json`](https://github.com/loadsheet/readerly/blob/main/test/golden/json.stdout.json) on stdout:

```json
{
  "schema": "acme.check/v1",
  "status": "failed",
  "summary": {
    "error": 2,
    "warn": 1,
    "info": 0
  },
  "diagnostics": [
    {
      "severity": "error",
      "code": "ACME001",
      "message": "Route handler reads process.env at module scope",
      "why": "The value is fixed at build time",
      "fix": "Read it inside the handler",
      "file": "src/app.ts",
      "line": 14,
      "column": 3
    },
    {
      "severity": "error",
      "code": "ACME004",
      "message": "Top-level await in a server entry",
      "fix": "Move the call into the plugin setup",
      "file": "src/db.ts",
      "line": 3,
      "column": 1
    },
    {
      "severity": "warn",
      "code": "ACME010",
      "message": "Alias shadows a workspace package",
      "file": "vite.config.ts",
      "line": 8,
      "column": 5
    }
  ],
  "result": {
    "files": 212,
    "durationMs": 840
  },
  "next": [
    {
      "command": "acme check --fix",
      "reason": "applies 2 safe fixes"
    },
    {
      "command": "acme explain <code>",
      "reason": "explains one code"
    }
  ]
}
```

| Field | What it holds |
| --- | --- |
| `schema` | The document's name and version. |
| `status` | `"ok"`, `"failed"` or `"action_required"`. |
| `summary` | The counts, as `{ error, warn, info }`. |
| `diagnostics` | Every [`DiagnosticRecord`](https://readerly.loadsheet.dev/guide/diagnostics#the-record), in the order you added them, with `data` if you set it. |
| `result` | What you passed to `out.set()`. |
| `next` | Each `out.next()` as `{ command, reason }`, plus the `next:` lines skipped prompts add. |
| `answer` | Only when the command called `out.write()`. |

The document is `JSON.stringify` with two-space indentation and a trailing newline. Logs from `out.log()` are dropped. There is no row cap and no budget, so `diagnostics` holds every record.

The `confirm` that asks a person at a terminal `? Apply 2 safe fixes? (y/N) ` doesn't run here. Its default answers, and the flag that changes it shows up in `next` as `acme check --fix`. See [Prompts and input](https://readerly.loadsheet.dev/guide/prompts).

## The schema name

`schema` is `<tool>.<command>/v1`, built from the `tool` and `command` you pass to `createOut`. Without a `command` it is `<tool>.run/v1`. Pass `schema` to name it yourself:

```ts [check.ts]
const out = createOut({ tool: "acme", command: "check", schema: "acme.check/v2", argv });
```

Change the version when you change the shape of `result`, so programs that read it can tell.

## Filling result

`out.set(fields)` merges `fields` into `result` with `Object.assign`. Call it as often as you like. The generic on `createOut` types the fields:

```ts [check.ts]
const out = createOut<{ files: number; durationMs: number }>({ tool: "acme", command: "check", argv });
out.set({ files: 212 });
out.set({ durationMs: 840 });
```

`result` starts as `{}`. `text` and `agent` print it only through the `summary` function you pass to `createOut`, such as `in 212 files (0.8s)`.

## The jsonl stream

A program that runs the golden `acme check --format jsonl` gets [`jsonl.stdout.jsonl`](https://github.com/loadsheet/readerly/blob/main/test/golden/jsonl.stdout.jsonl):

```text
{"type":"version","schema":"acme.check/v1","tool":"acme@1.2.0"}
{"type":"diagnostic","severity":"error","code":"ACME001","message":"Route handler reads process.env at module scope","why":"The value is fixed at build time","fix":"Read it inside the handler","file":"src/app.ts","line":14,"column":3}
{"type":"diagnostic","severity":"error","code":"ACME004","message":"Top-level await in a server entry","fix":"Move the call into the plugin setup","file":"src/db.ts","line":3,"column":1}
{"type":"diagnostic","severity":"warn","code":"ACME010","message":"Alias shadows a workspace package","file":"vite.config.ts","line":8,"column":5}
{"type":"next","message":"acme check --fix","reason":"applies 2 safe fixes"}
{"type":"next","message":"acme explain <code>","reason":"explains one code"}
{"type":"result","status":"failed","message":"2 errors, 1 warning in 212 files (0.8s)","data":{"files":212,"durationMs":840}}
```

Every record is one line of JSON with `type` first. The rules follow Terraform's machine-readable UI: a `version` record first, a `type` and a readable `message` on every record, and readers ignore types they don't know.

| `type` | When | Fields |
| --- | --- | --- |
| `version` | First, with the first entry | `schema`, `tool` as `name@version`, or `name` when you pass no `version` |
| `diagnostic` | Each `out.error`, `warn`, `info` or `diagnostic` | The record's fields |
| `log` | Each `out.log()` | `message` |
| `next` | Each `out.next()` | `message` (the command), `reason` |
| `error` | On a failure | The error fields, `missing`, `rerun` |
| `answer` | At the end, before `result`, when the command called `out.write()` | `message` |
| `result` | Last | `status`, `message`, `data` |

readerly writes each entry record the moment you add it, so a program can show progress while the command runs. The `result` record comes at `out.end()`. Its `message` is the verdict text, counts and summary, and `data` is `result`.

Unlike `json`, `jsonl` keeps logs. A diagnostic's own fields never replace `type`.

## Failures

On a failure, the json document drops the run fields and keeps what a caller needs to act: `schema`, `status`, `error`, and `missing` and `rerun` when the failure has them. It still goes to stdout, so a program reads one document whatever happened. A program that runs `acme deploy --prod --json` without an account gets this, with exit code 2:

```json
{
  "schema": "acme.deploy/v1",
  "status": "action_required",
  "error": {
    "code": "ACME_INPUT",
    "fix": "Pass --account.",
    "message": "Account is required."
  },
  "missing": [
    {
      "field": "account",
      "flag": "--account",
      "env": "ACME_ACCOUNT",
      "choices": [
        "personal",
        "team"
      ]
    }
  ],
  "rerun": "acme deploy --prod --account <account>"
}
```

A failure's own `schema`, set through `failure()`, replaces the run's. A thrown `Error("x")` from `acme check --json` prints `{ "schema": "acme.check/v1", "status": "failed", "error": { "message": "x" } }`, pretty-printed, with exit code 1 and stderr empty.

In `jsonl`, a failure adds an `error` record before `result`. A program that runs a command whose disk filled up gets:

```text
{"type":"version","schema":"acme.run/v1","tool":"acme"}
{"type":"error","message":"disk full"}
{"type":"result","status":"failed","message":"disk full","data":{}}
```

The `result` record's `message` is the error message. `data` is `result`, or `{}` when `result` can't be serialized, so the error still goes out.

## What a parser can rely on

- Nothing but the document or the records on stdout. `out.progress()` prints nothing, and `--verbose` and `out.debug()` lines go to stderr.
- No ANSI escapes. Color is off for `json` and `jsonl` whatever `--color` or `FORCE_COLOR` say.
- No prompts and no waiting on stdin, unless the caller passes `--input @-`.
- `status` and the exit code agree. `ok` is 0 and `action_required` is 2. `failed` is 1, or 2 for a usage failure, or a code the tool set with `exitCode`.

Next: [Custom renderers](https://readerly.loadsheet.dev/guide/renderers).

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://readerly.loadsheet.dev/raw/guide.md)
- [Installation](https://readerly.loadsheet.dev/raw/guide/installation.md)
- [Readers](https://readerly.loadsheet.dev/raw/guide/readers.md)
- [Coding agents](https://readerly.loadsheet.dev/raw/guide/agents.md)
- [Formats](https://readerly.loadsheet.dev/raw/guide/formats.md)
- [Shared flags](https://readerly.loadsheet.dev/raw/guide/flags.md)
- [Color and glyphs](https://readerly.loadsheet.dev/raw/guide/color.md)
- [Diagnostics](https://readerly.loadsheet.dev/raw/guide/diagnostics.md)
- [Budget](https://readerly.loadsheet.dev/raw/guide/budget.md)
- [Prompts and input](https://readerly.loadsheet.dev/raw/guide/prompts.md)
- [Failures and exit codes](https://readerly.loadsheet.dev/raw/guide/failures.md)
- [JSON and JSONL](https://readerly.loadsheet.dev/raw/guide/json.md) (this page)
- [Custom renderers](https://readerly.loadsheet.dev/raw/guide/renderers.md)
- [API](https://readerly.loadsheet.dev/raw/guide/api.md)

Next: [Custom renderers](https://readerly.loadsheet.dev/raw/guide/renderers.md)

All pages: [llms.txt](https://readerly.loadsheet.dev/llms.txt)
