JSON protocol
With --json, every command answers with one JSON object, for scripts, CI
jobs, the GUI and AI agents. This page describes that answer: the fields every
answer has, the exit status, which stream gets what, and the fields of each
command’s result. How to use it from a script is in Scripting; the
error cases are explained in Error reporting.
The exact format of each command’s answer is a JSON Schema (2020-12), in the
source repository under
packages/cli/docs/spec/04-protocol/: <command>.schema.json, with
spaces as - (list-parent.schema.json). common.schema.json has the
shapes they share. For a command shared by several backends, validate against
the Linux backend’s definition, $defs/linux (read.schema.json#/$defs/linux).
Turning it on
--json can be anywhere on the command line before a --:
analog-attach --json read accel and analog-attach read accel --json are
the same. In JSON mode the CLI never asks a question.
Streams
Stream |
What it gets |
|---|---|
Standard output |
At most one line: the JSON answer. Nothing else. |
Standard error |
Notes and warnings (the bus |
Exit status
Status |
When |
Standard output |
|---|---|---|
0 |
The command answered: a success, a refusal, or a configuration error.
Also |
The JSON answer ( |
1 |
The command failed without answering (an unexpected error). |
Nothing, or what the command wrote before failing; no answer. |
2 |
An input error: unknown command, flag or setting key, a missing or extra
argument, a malformed path. Also |
Nothing. The message is on standard error. |
130 |
A question was stopped (not in JSON mode). |
A line saying so. |
Status 0 doesn’t mean success: read ok.
The answer
Every answer is a JSON object with these fields, and the command’s own fields next to them:
Field |
Type |
Meaning |
|---|---|---|
|
boolean |
|
|
string |
What happened, in words: |
|
|
|
|
string |
On a refusal, and on a success whose |
A success:
{"ok": true, "message": "Set interrupts on accel", "severity": "info"}
A refusal:
{"ok": false, "message": "Not found: nope", "severity": "error", "error_type": "not-found"}
A success that warned, here the preview of delete without --force:
{"ok": true, "message": "Would delete 1 node(s) and 4 property(ies)", "severity": "warn",
"error_type": "needs-force", "node_count": 1, "property_count": 4, "paths": [["adxl345@1d"]]}
(Wrapped here; the CLI writes one line.)
Node paths are arrays of node names from the root, without the root:
/soc/i2c@7e804000/adxl345@1d is ["soc", "i2c@7e804000", "adxl345@1d"].
Configuration errors
When the project isn’t set up for the command (a setting missing or invalid,
the workfile missing, a tool missing), the command refuses with two more
fields: errors, one entry per problem, and hints, how to fix them.
error_type is the first problem’s:
{"ok": false, "message": "Missing config field linux", "severity": "error",
"error_type": "missing-setting",
"errors": [{"field": "linux", "kind": "missing", "error_type": "missing-setting",
"message": "Missing config field linux",
"description": "Path to Linux kernel source tree (setting it builds the binding caches)"}],
"hints": ["Run 'analog-attach' on a terminal, or 'analog-attach config linux <path>', to set up a workspace."]}
Each entry of errors has field (the setting, tool or script),
kind (missing, invalid, unknown), error_type,
message and, when there is one, description. The shape is
common.schema.json#/$defs/config-refusal.
When a settings file can’t be read (it isn’t JSON, or has an unknown key),
config writes {"errors": […], "hints": […]} to standard error and
exits with status 2 (config-error.schema.json).
Error types
error_type is one of a fixed list, the same for every backend, so that a
script can act on the case rather than on the wording. Those the Linux backend
gives:
|
Case |
|---|---|
|
A node, property, parent, overlay, example or variable that isn’t there. |
|
Several overlays have the name given. |
|
A compatible no binding has. |
|
A node of the base devicetree that |
|
A node name, label or variable name that isn’t allowed. |
|
A name or label already used under that parent. |
|
A value |
|
A |
|
A property written without a binding to check it (a warning). |
|
A value or count the binding doesn’t allow (a warning of |
|
A required property is missing (a finding of |
|
An address or chip select already used. |
|
A reference or fragment target that names no node; a device on a
disabled parent ( |
|
The |
|
dtc, dt-validate, the preprocessor, |
|
The base devicetree or the workfile doesn’t parse. |
|
|
|
|
|
A binding that can’t be narrowed down for the node. |
|
Configuration errors; see Error reporting. |
|
A value outside a fixed set of choices (a skill, a shell). |
The complete list is the error-type definition in common.schema.json.
Input errors (exit status 2) have no JSON and no error type.
Results by command
The fields each command adds to ok, message and severity on
success. The schema file named in each row has the details.
Command |
Fields |
Schema |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
The whole overlay, a node ( |
|
|
A set: no more fields. A read: as |
|
|
The preview: |
|
|
None. |
|
|
|
|
|
|
|
|
Without a path: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
None. |
|
|
|
|
|
|
|
When build or deploy fails, the refusal’s message is Build
failed: AA_BUILD exited with status <n> (or Deploy failed: …), followed
by a newline and what the command wrote to standard error.
analog-attach --json with no command answers usage (the usage text) and
missing (the required settings still unset).
Examples
$ analog-attach --json read accel reg
{"ok":true,"message":"Read reg of accel","severity":"info","kind":"property","key":"reg","type":{"kind":"array","items":{"kind":"number","subtype":"int"},"min_items":1,"max_items":1},"value":[29],"source":"overlay","written":["0x1d"],"description":"Chip select used by the device.","required":true,"definition":"adi,adxl345.yaml","dt_type":"address · 1 row of 1 cell · 0 to 256","suggestions":{"values":[{"value":"29","display_string":"0x1d — matches the unit address of adxl345@1d"}]}}
$ analog-attach --json list node accel
{"ok":true,"message":"1 node matches \"accel\"","severity":"info","nodes":[{"path":"/soc/i2c@7e804000/adxl345@1d","key":"adxl345@1d","labels":["accel"],"compatible":["adi,adxl345"],"origin":"internal","change":"added","has_children":false}]}
$ analog-attach --json validate
{"ok":true,"message":"0 error(s), 0 warning(s)","severity":"info","errors":[],"warnings":[]}
$ analog-attach --json build
{"ok":true,"message":"Built /home/user/work/rpi4-adxl345/overlay.dtso with AA_BUILD","severity":"info","path":"/home/user/work/rpi4-adxl345/build/overlay.dtbo"}