Tools
Get Pro

jq arrays: index, slice, iterate and length

Most real JSON is a list of things. These are the jq filters for getting into arrays — the building blocks for everything in the later lessons.

Get one item: .[n]

Arrays are zero-indexed. Negative indexes count from the end, so .[-1] is the last item.

.[0]Try it

Input

[
  {
    "name": "Ada",
    "age": 36,
    "team": "core"
  },
  {
    "name": "Linus",
    "age": 28,
    "team": "kernel"
  },
  {
    "name": "Grace",
    "age": 45,
    "team": "core"
  },
  {
    "name": "Ken",
    "age": 28,
    "team": "kernel"
  }
]

Output

{
  "name": "Ada",
  "age": 36,
  "team": "core"
}
jq '.[0]' data.json
.[-1].nameTry it

Input

[
  {
    "name": "Ada",
    "age": 36,
    "team": "core"
  },
  {
    "name": "Linus",
    "age": 28,
    "team": "kernel"
  },
  {
    "name": "Grace",
    "age": 45,
    "team": "core"
  },
  {
    "name": "Ken",
    "age": 28,
    "team": "kernel"
  }
]

Output

"Ken"
jq '.[-1].name' data.json

Slice a range: .[a:b]

A slice returns an array from index a up to (not including) b. Leave a side empty to go to the start or end.

.[1:3] | map(.name)CompactTry it

Input

[
  {
    "name": "Ada",
    "age": 36,
    "team": "core"
  },
  {
    "name": "Linus",
    "age": 28,
    "team": "kernel"
  },
  {
    "name": "Grace",
    "age": 45,
    "team": "core"
  },
  {
    "name": "Ken",
    "age": 28,
    "team": "kernel"
  }
]

Output

["Linus","Grace"]
jq -c '.[1:3] | map(.name)' data.json

Iterate: .[]

.[] outputs every item as a separate result. Everything after a | then runs once per item.

.[] | .nameTry it

Input

[
  {
    "name": "Ada",
    "age": 36,
    "team": "core"
  },
  {
    "name": "Linus",
    "age": 28,
    "team": "kernel"
  },
  {
    "name": "Grace",
    "age": 45,
    "team": "core"
  },
  {
    "name": "Ken",
    "age": 28,
    "team": "kernel"
  }
]

Output

"Ada"
"Linus"
"Grace"
"Ken"
jq '.[] | .name' data.json

Collect results: [ ... ]

Wrap a filter in square brackets to gather its separate outputs back into one array.

[.[] | .name]CompactTry it

Input

[
  {
    "name": "Ada",
    "age": 36,
    "team": "core"
  },
  {
    "name": "Linus",
    "age": 28,
    "team": "kernel"
  },
  {
    "name": "Grace",
    "age": 45,
    "team": "core"
  },
  {
    "name": "Ken",
    "age": 28,
    "team": "kernel"
  }
]

Output

["Ada","Linus","Grace","Ken"]
Same result as map(.name). jq -c '[.[] | .name]' data.json

Count items: length

length counts array items, object keys or string characters.

lengthTry it

Input

[
  {
    "name": "Ada",
    "age": 36,
    "team": "core"
  },
  {
    "name": "Linus",
    "age": 28,
    "team": "kernel"
  },
  {
    "name": "Grace",
    "age": 45,
    "team": "core"
  },
  {
    "name": "Ken",
    "age": 28,
    "team": "kernel"
  }
]

Output

4
jq 'length' data.json
.users[] | {name, orders: (.orders | length)}CompactTry it

Input

{
  "users": [
    {
      "id": 1,
      "name": "Ada",
      "email": "ada@example.com",
      "active": true,
      "country": "UK",
      "tags": [
        "admin",
        "dev"
      ],
      "orders": [
        {
          "id": "o1",
          "total": 30
        },
        {
          "id": "o2",
          "total": 12.5
        }
      ]
    },
    {
      "id": 2,
      "name": "Linus",
      "email": "linus@example.com",
      "active": false,
      "country": "FI",
      "tags": [
        "dev"
      ],
      "orders": []
    },
    {
      "id": 3,
      "name": "Grace",
      "email": "grace@example.com",
      "active": true,
      "country": "US",
      "tags": [
        "dev",
        "ops"
      ],
      "orders": [
        {
          "id": "o3",
          "total": 99
        }
      ]
    }
  ]
}

Output

{"name":"Ada","orders":2}
{"name":"Linus","orders":0}
{"name":"Grace","orders":1}
jq -c '.users[] | {name, orders: (.orders | length)}' data.json

First, last and nth

first, last and nth(n) read like plain English. first(f) also stops a filter after its first result.

[first, last] | map(.name)CompactTry it

Input

[
  {
    "name": "Ada",
    "age": 36,
    "team": "core"
  },
  {
    "name": "Linus",
    "age": 28,
    "team": "kernel"
  },
  {
    "name": "Grace",
    "age": 45,
    "team": "core"
  },
  {
    "name": "Ken",
    "age": 28,
    "team": "kernel"
  }
]

Output

["Ada","Ken"]
jq -c '[first, last] | map(.name)' data.json

Common mistakes

  • .[] produces several results, not an array. Wrap it in [ ] when you need an array back.
  • .[0] on an object is an error — use .key or to_entries for objects.

Practice

Open the jq Playground with your own JSON — it runs the real jq 1.8 in your browser, so nothing is uploaded. The jq cheat sheet lists every filter from these lessons on one page.