Skip to content
Latchkey

jq: Usage, Options & Common CI Errors

jq slices, filters, and transforms JSON with a small expression language.

jq is how pipelines read API responses. The two CI essentials are -r (raw strings, no quotes) and -e (exit-code on null/false), which turn jq into a reliable gate.

What it does

jq applies a filter program to JSON input and emits JSON (or raw text with -r) on stdout. It is the standard tool for extracting fields from API responses in scripts.

Common usage

Terminal
echo '{"name":"ci"}' | jq -r '.name'        # -> ci (no quotes)
jq -r '.items[].id' response.json
jq '.[] | select(.active)' data.json
jq -n --arg v "${VALUE}" '{key: $v}'        # safely inject a var
curl -fsS https://api/x | jq -e '.ok' >/dev/null || exit 1

Options

FlagWhat it does
-r / --raw-outputEmit raw strings without JSON quotes
-e / --exit-statusExit 1 if last output is null/false, 4 if no output
-n / --null-inputStart with null input (build JSON)
--arg name valuePass a shell string as a $name variable
-c / --compact-outputOne JSON object per line
-s / --slurpRead the whole input stream into one array

Common errors in CI

jq: error (at <stdin>:0): Cannot index string with "x" - you indexed a value that is not an object (often the input was an error string, not JSON). "parse error: Invalid numeric literal at line 1, column N" means the input was not JSON at all (an HTML error page or empty body) - pipe curl -f so failures do not reach jq. A missing key yields null silently; use -e to fail, and never interpolate untrusted values into the filter (use --arg).

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →