Skip to content

CLI Reference ​

text
aq [OPTIONS] [EXPRESSION] [FILES]...
  • With no EXPRESSION, aq runs ..
  • With no FILES, aq reads stdin. Use - to read stdin explicitly alongside files; it may appear at most once.
  • Results are a jq-style stream: each result is printed separately and nothing is collected into an array unless you ask for it with --slurp.
  • JSON and JSONL inputs are evaluated one value at a time as they are parsed, like jq. If a later value is malformed, the results for earlier values are printed first and then aq exits with status 5. Other formats are parsed in full before evaluation starts. TOML and table output wait for every result, since they render all results together.
  • A runtime error, such as error(...) or adding a string to a number, is printed and evaluation moves on to the next input, as in jq. Only a parse error stops reading input.
  • JSONL input over 4 MB is evaluated on every core when the query handles each input on its own: no input, inputs, or debug, no -s or -n, and JSON or JSONL output. Results and errors come out exactly as they would on one thread.

Formats ​

FormatReadWriteNotes
jsonyesyesInput may be a single value or a whitespace-separated stream of values, like jq.
jsonlyesyesOne value per line; blank lines are ignored.
yamlyesyesMulti-document input and output. Scalar mapping keys such as 200: are read as strings. .inf/.nan are floats.
tomlyesyesOutput must be exactly one object. Local dates, times, and datetimes round-trip as TOML values and render as strings in other formats.
csvyesyesEach row is an array of strings. Each output result must be an array of scalars.
tsvyesyesLike CSV, with backslash escapes for tab, newline, carriage return, and backslash.
tablenoyesHuman-readable output only. Objects become keyed columns, arrays indexed columns, scalars a single value column.

Choosing the input format ​

-p/--input-format forces a reader. Otherwise aq uses the file extension and falls back to sniffing the content. When the extension and the content disagree, --detect-conflicts (or AQ_DETECT_CONFLICTS) decides:

PolicyBehavior
warn-fallback (default)Parse with the extension's format. Only if that fails, warn and use the sniffed format.
extensionAlways use the extension's format.
sniffAlways use the sniffed format.

Stdin has no extension, so it is always sniffed. Input that starts with { or [ and has no YAML --- line is read as JSON (or JSONL, when every line is a record); if its first value fails to parse, detection falls back to the full sniffer. The sniffer checks for JSONL, a JSON value stream, TOML, multi-row TSV and CSV, and otherwise reads YAML.

Choosing the output format ​

-o/--output-format picks the writer. Without it, aq writes the input format when all inputs share one, and JSON otherwise.

Shape rules:

  • json, jsonl, and yaml can emit any number of results.
  • toml requires exactly one object result.
  • csv and tsv treat each result as one row and require arrays of scalars.
  • table rejects -r, -j, and --raw-output0.

CSV and TSV values ​

--tabular-coercion strings (the default) keeps every cell as a string, so zip codes and IDs survive untouched. --tabular-coercion infer-scalars turns cells that are exact JSON scalars (true, null, 1.25) into those values.

bash
aq -o json -c . people.csv                                  # ["1","alice"]
aq -o json -c --tabular-coercion infer-scalars . people.csv # [1,"alice"]

Input options ​

FlagMeaning
-p, --input-format <FMT>Force the input reader. (-f FMT still works when no file named FMT exists, with a deprecation warning.)
-f, --from-file FILERead the query from FILE, as jq does. The first positional argument is then an input file.
-s, --slurpRead every input document into one array before evaluating.
-R, --raw-inputTreat each input line as a string.
-n, --null-inputEvaluate once against null. File arguments stay readable through input/inputs; stdin is read only if the query uses them.
--streamTurn each input into jq's [path, leaf] events, as tostream does.
--seqAccept RFC 7464 input (each value preceded by RS, 0x1e) and write RS before each JSON result.
--line-streamEvaluate JSONL or --raw-input one line at a time as it arrives; see below.
--tabular-coercion <MODE>strings or infer-scalars for CSV/TSV input.

Output options ​

FlagMeaning
-o, --output-format <FMT>Output writer.
-c, --compactCompact rendering where the format supports it.
-r, --raw-outputPrint strings without JSON quoting.
-j, --join-outputRaw output with no newline between results.
--raw-output0Raw output with each result terminated by NUL.
-S, --sort-keysSort object keys.
--indent <N> / --tabJSON indentation; --indent -1 means tabs.
-a, --ascii-outputEscape every non-ASCII character as \uXXXX. As in jq, strings print as JSON even with -r.
--unbufferedFlush after each result.
-C, --color-output / -M, --monochrome-outputForce or disable color. Color is automatic on a TTY unless NO_COLOR is set.

Numbers keep their written precision: big integers and long decimals are not rounded through doubles. As in jq 1.8, infinities print as ±1.7976931348623157e+308 and NaN prints as null.

Query variables ​

These work like jq's flags of the same names: --arg NAME VALUE, --argjson NAME JSON, --rawfile NAME PATH, --slurpfile NAME PATH, and --args / --jsonargs to turn the remaining arguments into $ARGS.positional. -L, --library-path DIR adds a directory to the module search path for import and include.

In-place rewrites ​

-i, --in-place rewrites each file argument with the query's result.

  • Every file is evaluated and rendered first. Files are replaced only if all of them succeed, so a bad file leaves every file untouched.
  • Each file is written to a temporary file next to it and renamed into place. The original permissions are kept.
  • A symlink is followed: the target file is rewritten and the link stays a link.
  • A query that produces no results is an error and the file is left unchanged.
  • JSON and TOML rewrites need exactly one result; YAML can write several documents.
  • Comments and formatting are not preserved.
  • -i rejects raw output modes, table output, -e, and stdin.
bash
aq -i '.version = "2.0"' Cargo.toml
aq -i 'del(.metadata.annotations)' k8s/*.yaml

Streaming ​

--line-stream evaluates the query once per JSONL line (or per raw line with -R) and prints results as it goes, so it works on unbounded input. The full query language is available inside each record. It rejects input/inputs, --null-input, and YAML, TOML, or table output.

bash
tail -f access.jsonl | aq --line-stream -c 'select(.status >= 500) | {path, status}'

Exit status ​

CodeMeaning
0Success.
1With -e: the last result was false or null. Also unsupported flag combinations and other general errors.
2Unknown flag or invalid flag value, or a file could not be read or written.
3The expression failed to parse.
4With -e: there were no results.
5The last input failed with a runtime error (error(...), a failed operation), or input could not be parsed.

These match jq. Writing into a closed pipe, as in aq . big.json | head, exits 0 silently.

Environment ​

VariableMeaning
AQ_FLAGSShell-style flags prepended to the command line, e.g. AQ_FLAGS="-c -S".
AQ_DETECT_CONFLICTSDefault for --detect-conflicts.
NO_COLORDisable automatic color.

Other options ​

  • --explain prints the parsed query and exits.
  • -e, --exit-status sets the exit code from the last result (see above).
  • --generate-completions <SHELL> prints completions for bash, zsh, fish, elvish, or PowerShell.
  • -F, -P, --starlark and the other --starlark-* flags are covered in Starlark.

aq-specific builtins ​

On top of jq's builtins, aq adds a few conveniences:

GroupBuiltins
Conversionto_number, to_bool, to_datetime
Statisticsavg, median, stddev, percentile(p), histogram(n)
Collectionstake(n), skip(n), columns, uniq_by(f), sort_by_desc(f), count_by(f)
Objectsmerge(obj), merge_all(deep), drop_nulls, sort_keys, pick(paths), omit(paths), rename(path; name)
Format metadatayaml_tag, csv_header, xml_attr
bash
aq -c '[.[] | .age] | {avg: avg, p90: percentile(90)}' users.json
aq --slurp 'merge_all(true)' base.yaml env.yaml local.yaml
aq -c '.[] | omit(.password)' users.json

Structured querying, rewriting, and Starlark scripting across common data formats.