mikefarah/yq · error

cannot convert node at path %v of tag %v to number

Error message

cannot convert node at path %v of tag %v to number

What it means

The to_number operator (to_number) can only convert scalar nodes. If the matched node is a mapping or sequence, toNumberOperator aborts with this error, reporting the node's path and tag, because there is no numeric interpretation of a compound node.

Source

Thrown at pkg/yqlib/operator_to_number.go:33

	// try float
	_, floatErr := strconv.ParseFloat(value, 64)

	if floatErr == nil {
		return "!!float", true
	}
	return "", false

}

func toNumberOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
	log.Debugf("ToNumberOperator")

	var results = list.New()

	for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
		candidate := el.Value.(*CandidateNode)
		if candidate.Kind != ScalarNode {
			return Context{}, fmt.Errorf("cannot convert node at path %v of tag %v to number", candidate.GetNicePath(), candidate.Tag)
		}

		if candidate.Tag == "!!int" || candidate.Tag == "!!float" {
			// it already is a number!
			results.PushBack(candidate)
		} else {
			tag, converted := tryConvertToNumber(candidate.Value)
			if converted {
				result := candidate.CreateReplacement(ScalarNode, tag, candidate.Value)
				results.PushBack(result)
			} else {
				return Context{}, fmt.Errorf("cannot convert node value [%v] at path %v of tag %v to number", candidate.Value, candidate.GetNicePath(), candidate.Tag)
			}

		}
	}

	return context.ChildContext(results), nil

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Narrow the expression to scalar fields only, e.g. `.data.count | to_number` instead of `.data | to_number`
  2. Pre-filter non-scalars: `[.[] | select(tag == "!!str" or tag == "!!int" or tag == "!!float")] | map(to_number)`
  3. Convert compound nodes to a scalar first (e.g. length, tostring) before calling to_number

Example fix

// before: applies to maps too
.items[] | to_number
// after: target the scalar field
.items[].count | to_number
Defensive patterns

Strategy: type-guard

Validate before calling

// only convert scalars
.data.count | select(kind == "scalar") | to_number

Type guard

// expression-level guard
def isScalar: kind == "scalar";
.<field> | select(isScalar) | to_number

Prevention

When it happens

Trigger: `yq '.items[] | to_number'` where an element of .items is a map or array, or applying to_number to the document root when the root is an object: `yq 'to_number'` on a YAML file containing a mapping.

Common situations: to_number applied to a list of records where some entries are nested objects, or selecting too broadly (e.g. `.data[]` matching sub-objects) instead of the specific scalar fields.

Related errors


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