GitHub
Skip to content

JSON and JSONL

The json document and the jsonl record stream a readerly command prints, their fields and schema name, failures in both, and how out.set fills result.

--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.

A program that runs the golden acme check --json gets json.stdout.json on stdout:

{
  "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"
    }
  ]
}
json
FieldWhat it holds
schemaThe document's name and version.
status"ok", "failed" or "action_required".
summaryThe counts, as { error, warn, info }.
diagnosticsEvery DiagnosticRecord, in the order you added them, with data if you set it.
resultWhat you passed to out.set().
nextEach out.next() as { command, reason }, plus the next: lines skipped prompts add.
answerOnly 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.

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:

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

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

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

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

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

A program that runs the golden acme check --format jsonl gets jsonl.stdout.jsonl:

{"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}}
text

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.

typeWhenFields
versionFirst, with the first entryschema, tool as name@version, or name when you pass no version
diagnosticEach out.error, warn, info or diagnosticThe record's fields
logEach out.log()message
nextEach out.next()message (the command), reason
errorOn a failureThe error fields, missing, rerun
answerAt the end, before result, when the command called out.write()message
resultLaststatus, 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.

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:

{
  "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>"
}
json

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:

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

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.

  • 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.