jq .[] : Iterate Over a JSON Array or Object
jq .[] emits every element of an array (or every value of an object) as a separate output.
Most jq pipelines start by exploding a JSON array from gh api or curl so later filters run once per item.
What it does
The .[] operator iterates a structure: applied to an array it outputs each element, applied to an object it outputs each value. It produces a stream of results, one per element, which downstream filters then process individually.
Common usage
gh api repos/cli/cli/tags --jq '.[].name'
# explode an array of objects, one field each
curl -s https://api.github.com/repos/cli/cli/tags | jq '.[].name'
# iterate values of an object
echo '{"a":1,"b":2}' | jq '.[]'Operators
| Form | What it does |
|---|---|
| .[] | Iterate every element of an array or value of an object |
| .foo[] | Iterate the array at key foo |
| .[0] | Index a single array element |
| .[]? | Iterate but suppress errors on non-iterables |
In CI
Pair .[] with -r so each emitted string is printed without quotes, ready to loop over in a shell for or pipe into another command in a workflow step.
Common errors in CI
"jq: error: Cannot iterate over null (null)" means the key you indexed before .[] does not exist, so .[] ran on null; check the path or use .[]? to tolerate it. "Cannot iterate over string" means the value is a scalar, not an array; an API may have returned an error object instead of the list you expected.