[jq] Limit the number of results emitted by a filter

Created on November 30, 2026

Use limit(maximum; filter) to emit up to a given number of results. This example selects numbers of 20 or greater, then emits at most the first two matches.

printf '%s\n' '[10,20,30,40]' | jq 'limit(2; .[] | select(. >= 20))'
20
30

If the filter finds fewer matches, jq emits only those values.

Collect the results in an array

Wrap the filter in brackets to create one array instead of emitting the results separately.

printf '%s\n' '[10,20,30,40]' | jq '[limit(2; .[] | select(. >= 20))]'
[
  20,
  30
]

Reference

jq