jq Command Reference for CI Scripts
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.
Common flags/usage
- -r / --raw-output: emit raw strings without JSON quotes
- -e / --exit-status: exit 1 if the last output is null or false
- --arg NAME VALUE: pass a shell string as a $NAME variable safely
- -n / --null-input: build JSON from scratch with no input
- -c / --compact-output: one JSON object per line
Example
shell
TAG=$(curl -fsS "https://api.github.com/repos/${REPO}/releases/latest" \
| jq -r '.tag_name')
echo "Latest tag: ${TAG}"
jq -n --arg sha "${GITHUB_SHA}" '{commit: $sha}' > build-meta.jsonIn CI
Pipe curl -f into jq so an HTML error page never reaches the parser (otherwise you get "parse error: Invalid numeric literal"). A missing key yields null silently, so use -e to fail a gate. Never interpolate untrusted values into a filter; pass them with --arg.
Key takeaways
- -r strips JSON quotes so output is usable directly in shell variables.
- Pass shell values with --arg, never by string-interpolating into the filter.
- Use -e to make jq fail the step when a key is missing or false.
Related guides
curl Command Reference for CI Scriptscurl transfers data over HTTP(S) in CI scripts. Reference for -f, -sS, -L, -o, and --retry so downloads fail…
sed Command Reference for CI Scriptssed is a stream editor for find-and-replace in CI builds. Reference for -i, -e, -E, and s///, plus the GNU-vs…
grep Command Reference for CI Scriptsgrep searches text for matching lines in CI. Reference for -r, -E, -i, -q, and -v, plus the exit-code-1-means…
envsubst Command Reference for CI Scriptsenvsubst substitutes environment variables into templates in CI. Reference for variable lists and the over-su…