jq · lesson 4 of 32 · Getting started

Iterate & collect

.[] turns an array into a stream of its elements — one output each. Wrap any filter in [ … ] to collect a stream back into a single array.

Examples

Example 1Stream every member's name

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[].name
  1. .members The array of members.
  2. [] Iterate: emit each element as its own output instead of the array.
  3. .name Read name from every element — four separate results.

Result

"Ada"
"Linus"
"Grace"
"Alan"

Try it in the playground

Example 2Collect a stream into one array

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[].age]
  1. [ Square brackets around a filter collect everything it outputs…
  2. .members[].age …here, every member's age…
  3. ] …into one array — a single result.

Result

[36,28,45,41]

Try it in the playground

Good to know

  • .[] also iterates over the values of an object: .office[] gives "Berlin" then 3.

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.