jq: Cannot index string with string
Updated by John Marshall
jq stops with Cannot index string with string "name" when your filter asks for a key, such as .name, on a value that isn’t an object. The first type in the message is what jq found at that step. If it’s array, the value is a list and you need .[] before the key. If it’s string, your path goes one level deeper than the data, or the value is JSON stored as text.
Quick fix
Cannot index array with string: put.[]before the key to read it from every item, as in.[].name, or use.[0].namefor the first item.Cannot index string with string: the value before the key is a string. Remove the last step of the path. If the string contains JSON, parse it withfromjsonfirst.- To skip values that don’t have the key instead of stopping, add a question mark:
.name?.
Cannot index array with string
Many APIs return a list, or an object with a list inside. This file has two robots from a fleet API:
[
{
"id": "TIDY-002",
"name": "Dusty",
"location": { "floor": 2, "room": "open plan" },
"modes": ["vacuum"]
},
{
"id": "TIDY-005",
"name": "Socks",
"location": { "floor": 1, "room": "lobby" },
"modes": ["vacuum", "mop"]
}
].name fails because the top level of the file is a list, and a list has no keys:
$ jq '.name' robots.json
jq: error (at robots.json:14): Cannot index array with string "name".[] outputs the items one at a time, so .[].name prints one name per robot. map(.name) returns the names as a JSON array, and .[0].name returns the first one:
$ jq '.[].name' robots.json
"Dusty"
"Socks"
$ jq 'map(.name)' robots.json
[
"Dusty",
"Socks"
]
$ jq '.[0].name' robots.json
"Dusty"Deeper lists need [] too. If each robot had a list of runs, the filter would be .[].runs[].id.
The number after the file name, robots.json:14, is the input line jq had read up to when the error happened. jq reads a whole JSON document before running the filter, so for a file with one document it’s the last line of the file. It doesn’t tell you which part of the filter failed.
Cannot index string with string
Here the path goes one level deeper than the data. room is a string, so it has no keys:
$ jq '.[] | .location.room.name' robots.json
jq: error (at robots.json:14): Cannot index string with string "name"To find the failing step in a long filter, run it one step at a time and check each result with type. Fix the first step that returns something other than object:
$ jq '.[0] | type' robots.json
"object"
$ jq '.[0].location | type' robots.json
"object"
$ jq '.[0].location.room | type' robots.json
"string"You can also run the filter one step at a time in the jq playground. It runs in your browser and reports these errors in the jq 1.7 wording.
JSON stored inside a string
Logs and message queues often store JSON as a string inside other JSON. The escaped quotes in the value show this:
{"event": "stuck", "payload": "{\"robot\": \"TIDY-002\", \"stuck_under\": \"standing desk\"}"}To jq, payload is a string, so .payload.robot fails:
$ jq '.payload.robot' events.json
jq: error (at events.json:1): Cannot index string with string "robot"fromjson parses the string, and the rest of the path works:
$ jq '.payload | fromjson | .robot' events.json
"TIDY-002"Cannot iterate over null
A missing key isn’t an error in jq: .tags on a robot without tags returns null. The error happens when you use [] on that null:
$ jq '.[] | .tags[]' robots.json
jq: error (at robots.json:14): Cannot iterate over null (null)Check the key’s spelling first. If some items don’t have the list, either of these filters skips them:
jq '.[] | .tags[]?' robots.json
jq '.[] | (.tags // [])[]' robots.jsonThe first uses ? to suppress the error. The second replaces a missing or null list with an empty one, so the rest of the filter still gets a list.
Every version of the message
The first type is what jq found, and the second is what you indexed it with: a string for .name, a number for [0].
| Message | What jq found | What to change |
|---|---|---|
Cannot index array with string "name" | The value is a list. | Add .[] to go through every item, or .[0] for the first one. |
Cannot index string with string "name" | The value is a string. | Stop one step earlier, or use fromjson if the text is JSON. |
Cannot index number with string "name" | The value is a number, like a count or an ID. | Compare the path with the data. It goes one level too deep. |
Cannot index boolean with string "name" | The value is true or false. | The path goes past the end of the data. |
Cannot index object with number | You used [0] on an object. | Objects are indexed by key. Use .key, or .[] for every value. |
Cannot index string with number | You used [0] on text. | jq doesn’t index into strings. Use .[0:1] for the first character, or split first. |
jq 1.8.2 changed the wording. It adds the key or number in brackets at the end, as in Cannot index array with string ("name"), and the number form now shows the number. The causes are the same.
If jq reports a parse error instead, the input isn’t valid JSON and the filter never ran. See jq: parse error: Invalid numeric literal.
The same error elsewhere
| jq 1.8.2 and later | jq: error (at robots.json:14): Cannot index array with string ("name") |
|---|---|
| jq 1.8.2 and later, number index | jq: error (at robots.json:14): Cannot index string with number (0) |
| jq 1.6 to 1.8.1 | jq: error (at robots.json:14): Cannot index array with string "name" |
| jq, [] on a string | jq: error (at robots.json:14): Cannot iterate over string ("Dusty") |
| Python, a key on a list | TypeError: list indices must be integers or slices, not str |