{"record":{"id":"a8fbc60237e1d3f4","repo":"mikefarah/yq","slug":"cannot-index-array-with-v-w","errorCode":null,"errorMessage":"cannot index array with '%v' (%w)","messagePattern":"cannot index array with '(.+?)' \\(%w\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_traverse_path.go","lineNumber":219,"sourceCode":"\tvar newMatches = list.New()\n\tif len(indices) == 0 {\n\t\tlog.Debug(\"splatting\")\n\t\tvar index int\n\t\tfor index = 0; index < len(node.Content); index = index + 1 {\n\t\t\tnewMatches.PushBack(node.Content[index])\n\t\t}\n\t\treturn newMatches, nil\n\n\t}\n\n\tfor _, indexNode := range indices {\n\t\tlog.Debugf(\"traverseArrayWithIndices: '%v'\", indexNode.Value)\n\t\tindex, err := parseInt(indexNode.Value)\n\t\tif err != nil && prefs.OptionalTraverse {\n\t\t\tcontinue\n\t\t}\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"cannot index array with '%v' (%w)\", indexNode.Value, err)\n\t\t}\n\t\tindexToUse := index\n\t\tcontentLength := len(node.Content)\n\t\tfor contentLength <= index {\n\t\t\tif contentLength == 0 {\n\t\t\t\t// default to nice yaml formatting\n\t\t\t\tnode.Style = 0\n\t\t\t}\n\n\t\t\tvalueNode := createScalarNode(nil, \"null\")\n\t\t\tnode.AddChild(valueNode)\n\t\t\tcontentLength = len(node.Content)\n\t\t}\n\n\t\tif indexToUse < 0 {\n\t\t\tindexToUse = contentLength + indexToUse\n\t\t}\n","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_traverse_path.go#L201-L237","documentation":"When traversing an array with an index, yq parses the index string to an integer. If the index cannot be parsed as a number and the traversal is not optional (`?.`), it fails with 'cannot index array with ...'. The underlying parse error is wrapped (%w).","triggerScenarios":"Expressions like `.a[foo]` or `.a[\"1x\"]` where the supplied index token is not a valid integer and the path is not an optional traverse; also calling traverseArrayWithIndices via the API with a non-numeric indexNode.Value.","commonSituations":"Typo in an index expression, accidentally quoting an index, interpolating a variable that contains a non-numeric string, or using object-key syntax against an array node.","solutions":["Correct the expression so the index is an integer, e.g. `.a[0]` instead of `.a[foo]`","Use optional traverse `.[?]`-style syntax if you want non-numeric indexes to be skipped instead of erroring","If the intent is key lookup, verify the node is a map, not an array — reorder or add a type check in your expression, e.g. `select(type == \"seq\")`","Convert the value to a number first, e.g. `.a[($i | tonumber)]`"],"exampleFix":"// before\nyq '.items[foo]' file.yaml\n// after\nyq '.items[0]' file.yaml\n","handlingStrategy":"validation","validationCode":"// Ensure the index is a valid integer before building the path\ni, err := strconv.Atoi(idx)\nif err != nil {\n\treturn fmt.Errorf(\"array index %q is not an integer\", idx)\n}","typeGuard":"func isArray(node *yaml.Node) bool { return node != nil && node.Kind == yaml.SequenceNode }\nfunc isIntLiteral(s string) bool { _, err := strconv.Atoi(s); return err == nil }","tryCatchPattern":"out, err := yqEval(\".a[0]\", doc)\nif err != nil && strings.Contains(err.Error(), \"cannot index array\") {\n\t// handle non-numeric index: fall back to key lookup or skip\n}","preventionTips":["Always use integer literals for array indexing","Don't quote array indexes in yq expressions","Use optional traverse (`?`) where the index may be absent or non-numeric","Validate interpolated variables are numeric before embedding them in expressions"],"tags":["yaml","array","indexing","traversal"],"backgroundTag":"invalid-array-index","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}