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 itInput
[
{
"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 itInput
[
{
"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.jsonSlice 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 itInput
[
{
"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.jsonIterate: .[]
.[] outputs every item as a separate result. Everything after a | then runs once per item.
.[] | .nameTry itInput
[
{
"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.jsonCollect results: [ ... ]
Wrap a filter in square brackets to gather its separate outputs back into one array.
[.[] | .name]CompactTry itInput
[
{
"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"]map(.name). jq -c '[.[] | .name]' data.jsonCount items: length
length counts array items, object keys or string characters.
lengthTry itInput
[
{
"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
4jq 'length' data.json.users[] | {name, orders: (.orders | length)}CompactTry itInput
{
"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.jsonFirst, 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 itInput
[
{
"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.jsonCommon mistakes
.[]produces several results, not an array. Wrap it in[ ]when you need an array back..[0]on an object is an error — use.keyorto_entriesfor 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.