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)
  1. map( Run the filter inside on every element and collect the results (map(f) is short for [.[] | f]).
  2. select(.active) Drop inactive members — select may output nothing, and map simply collects fewer results.
  3. | .name Keep just the name.

Result

["Ada","Linus","Alan"]

Try it in the playground

Good to know

  • map_values(f) is the object version: it updates every value and keeps the keys.

About jsonquery

Query, format and learn JSON with jq, JSONPath, JMESPath and JSON Pointer. It is the engine of the desktop app, run in your browser.

Engine
The desktop app’s, compiled to WebAssembly
Licence
MIT (the engine and the app)
Source
GitHub
Contact
The contact form (private)
Public issues
GitHub
Legal
Privacy policy

Known limitations: a file over 10 MB opens read-only and one over 512 MB does not open here; the desktop app has no such limit.