Example input
[
{ "name": "Ada", "dept": "eng", "age": 36 },
{ "name": "Linus", "dept": "eng", "age": 54 },
{ "name": "Mae", "dept": "sci", "age": 28 }
]
Sort by a field
jq 'sort_by(.age)' data.json # ascending
jq 'sort_by(.age) | reverse' data.json # descending
Sort by multiple keys (array of criteria):
jq 'sort_by(.dept, .age)' data.json
Unique / dedupe
unique sorts and removes duplicates of scalar arrays:
echo '[3,1,2,1,3]' | jq 'unique' # [1,2,3]
Dedupe objects by a field with unique_by:
jq 'unique_by(.dept)' data.json # one object per dept
Group by a key
jq 'group_by(.dept)' data.json
Output: an array of arrays, one per dept.
Count per group
jq 'group_by(.dept) | map({ dept: .[0].dept, count: length })' data.json
Output:
[
{ "dept": "eng", "count": 2 },
{ "dept": "sci", "count": 1 }
]
Min / max by a field
jq 'max_by(.age)' data.json # the oldest record
jq 'min_by(.age)' data.json # the youngest record
group_by sorts first, so groups come out ordered by the key. To turn groups into a
{key: items} object, follow with map({ (.[0].dept): . }) | add. For top-N, sort_by(.age) | reverse | .[:3].