mikefarah/yq · error

%v: could not parse %v as an int: %w

Error message

%v: could not parse %v as an int: %w

What it means

Path arrays may only contain string keys or integer indices. When an element has tag !!int, yq re-parses its value with parseInt; if the underlying string cannot be parsed as an int (contradictory tag/value), the error wraps the parse failure, prefixed by the operator name (SETPATH/DELPATHS).

Source

Thrown at pkg/yqlib/operator_path.go:31

		return &CandidateNode{Kind: ScalarNode, Value: fmt.Sprintf("%v", pathElement), Tag: "!!int"}
	}
}

func getPathArrayFromNode(funcName string, node *CandidateNode) ([]interface{}, error) {
	if node.Kind != SequenceNode {
		return nil, fmt.Errorf("%v: expected path array, but got %v instead", funcName, node.Tag)
	}

	path := make([]interface{}, len(node.Content))

	for i, childNode := range node.Content {
		switch childNode.Tag {
		case "!!str":
			path[i] = childNode.Value
		case "!!int":
			number, err := parseInt(childNode.Value)
			if err != nil {
				return nil, fmt.Errorf("%v: could not parse %v as an int: %w", funcName, childNode.Value, err)
			}
			path[i] = number
		default:
			return nil, fmt.Errorf("%v: expected either a !!str or !!int in the path, found %v instead", funcName, childNode.Tag)
		}

	}
	return path, nil
}

// SETPATH(pathArray; value)
func setPathOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
	log.Debugf("SetPath")

	if expressionNode.RHS.Operation.OperationType != blockOpType {
		return Context{}, fmt.Errorf("SETPATH must be given a block (;), got %v instead", expressionNode.RHS.Operation.OperationType.Type)
	}

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Fix the source node so a !!int tag carries a valid integer value
  2. Use a !!str path element instead if the key is not an index
  3. In Go code, validate before constructing: strconv.Atoi(value) before setting Tag = "!!int"

Example fix

// Go, before
n := &yqlib.CandidateNode{Kind: yqlib.ScalarNode, Tag: "!!int", Value: "3x"}
// after
if _, err := strconv.Atoi("3x"); err != nil { /* use Tag: "!!str" or fix value */ }
n := &yqlib.CandidateNode{Kind: yqlib.ScalarNode, Tag: "!!str", Value: "3x"}
Defensive patterns

Strategy: type-guard

Validate before calling

// Go: before passing a node as an int path element
if n.Tag == "!!int" { if _, err := strconv.Atoi(n.Value); err != nil { /* fix tag/value */ } }

Type guard

func isIntPathElement(n *yqlib.CandidateNode) bool {
  if n.Tag != "!!int" { return false }
  _, err := strconv.Atoi(n.Value)
  return err == nil
}

Prevention

When it happens

Trigger: Rare in practice: a node tagged !!int whose Value is not numeric — typically created via custom scripts, constructor-style input, or manual CandidateNode construction in Go embedding, e.g. a node {tag: !!int, value: "3x"} passed as a path element.

Common situations: Programmatic use of the yqlib package where nodes are built by hand or transformed with tag-assignment operators without updating values; corrupted or hand-edited intermediate YAML/JSON with wrong explicit tags.

Related errors


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