jq · lesson 13 of 32 · Transforming
map & select together
map(f) runs f on every element and collects the results into a new array. Combine it with select to filter and transform in one step.
Examples
Example 1Names of the active members
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(select(.active) | .name)
map(Run the filter inside on every element and collect the results (map(f)is short for[.[] | f]).select(.active)Drop inactive members —selectmay output nothing, andmapsimply collects fewer results.| .nameKeep just the name.
Result
["Ada","Linus","Alan"]
Good to know
map_values(f)is the object version: it updates every value and keeps the keys.