jq · lesson 17 of 32 · Aggregation
Counting & math
Arrays have length, add, min and max, and the usual arithmetic works: + - * / %.
Examples
Example 1Count, total, average, extremes
Document
{
"team": "Platform",
"office": { "city": "Berlin", "floor": 3 },
"members": [
{ "name": "Ada", "age": 36, "role": "lead", "active": true,
"email": "[email protected]", "skills": ["rust", "sql"] },
{ "name": "Linus", "age": 28, "role": "dev", "active": true,
"email": null, "skills": ["c", "shell"] },
{ "name": "Grace", "age": 45, "role": "dev", "active": false,
"skills": ["cobol"] },
{ "name": "Alan", "age": 41, "role": "qa", "active": true,
"email": "[email protected]", "skills": [] }
]
}Query
.members | map(.age) | (length, add, add / length, min, max)
map(.age)An array of just the ages:[36,28,45,41].lengthHow many elements →4.addThe sum →150.add / lengthThe average →37.5.min, maxThe smallest and the largest →28,45.
Result
4 150 37.5 28 45
Good to know
min_by(.age)andmax_by(.age)return the whole element, not just the number.floorandroundare there for rounding.