mikefarah/yq · error

cannot index array with %v

Error message

cannot index array with %v

What it means

When `pick` is applied to an array, every entry of the indices array must parse as an integer, because array indices must be numeric. This error is thrown when `parseInt` fails on an index value (e.g. a string like "abc" or "1.5"). It prevents silently picking nothing or corrupting index semantics.

Source

Thrown at pkg/yqlib/operator_pick.go:34

			clonedKey := original.Content[indexInMap].Copy()
			clonedValue := original.Content[indexInMap+1].Copy()
			filteredContent = append(filteredContent, clonedKey, clonedValue)
		}
	}

	newNode := original.CopyWithoutContent()
	newNode.AddChildren(filteredContent)

	return newNode
}

func pickSequence(original *CandidateNode, indices *CandidateNode) (*CandidateNode, error) {

	filteredContent := make([]*CandidateNode, 0)
	for index := 0; index < len(indices.Content); index = index + 1 {
		indexInArray, err := parseInt(indices.Content[index].Value)
		if err != nil {
			return nil, fmt.Errorf("cannot index array with %v", indices.Content[index].Value)
		}

		if indexInArray > -1 && indexInArray < len(original.Content) {
			filteredContent = append(filteredContent, original.Content[indexInArray].Copy())
		}
	}

	newNode := original.CopyWithoutContent()
	newNode.AddChildren(filteredContent)

	return newNode, nil
}

func pickOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
	log.Debugf("Pick")

	contextIndicesToPick, err := d.GetMatchingNodes(context, expressionNode.RHS)

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Use integer indices when picking from arrays: `pick([0, 2])`.
  2. Check each index value parses as an int; convert with `tonumber` style expressions if derived from strings.
  3. If picking by key, ensure the node is a map — use `select(tag == "!!map")` or a different operator.
  4. Trim/format computed index expressions to avoid floats like `1.0`.

Example fix

// before
yq 'pick(["a", "b"])' file.json   # on an array
// after
yq 'pick([0, 1])' file.json
Defensive patterns

Strategy: validation

Validate before calling

yq -e 'map(tag == "!!int") | all' <<< "$indices" || echo "pick indices must all be integers"

Type guard

isIntArray() { [ "$(yq 'map(tag == "!!int") | all' <<< "$1")" = "true" ]; }

Try / catch

out=$(yq 'pick($idx)' file.yml 2>&1) || { echo "$out"; echo "pick on arrays requires integer indices"; }

Prevention

When it happens

Trigger: `yq 'pick(["a", "b"])'` on an array document, or `pick([0, "x"])` where a non-integer string appears in the indices sequence. Also occurs when the indices come from data containing non-integer scalars.

Common situations: Reusing a map-key pick expression on an array; indices built from string data; typos such as `pick([1o])` or values with whitespace/floats from computed expressions.

Related errors


AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05). Data as JSON: /api/errors/a4a20ed75e0e8d64. Report an issue: GitHub.