mikefarah/yq · error
cannot convert node value [%v] at path %v of tag %v to numbe
Error message
cannot convert node value [%v] at path %v of tag %v to number
What it means
The to_number operator found a scalar whose string value cannot be parsed as an int64 or float64. tryConvertToNumber fails (both parseInt64 and ParseFloat error), so toNumberOperator returns this error including the offending value, its path, and its tag.
Source
Thrown at pkg/yqlib/operator_to_number.go:45
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
- Clean/normalize the string before converting, e.g. remove non-numeric characters: `sub("[^0-9.]"; "") | to_number`
- For semver or composite values, extract the numeric part explicitly (e.g. `.version | sub("\\..*"; "") | to_number`)
- Handle commas/units: `gsub(","; "") | to_number` or strip units with sub("px"; "")
- Guard with a select/filter so only parseable values reach to_number
Example fix
// before: "1,000" fails to parse
count | to_number
// after: strip commas first
count | sub(","; "") | to_number Defensive patterns
Strategy: validation
Validate before calling
// strip non-numeric characters before conversion
count | sub("[^0-9.eE+-]"; ""; "g") | to_number Type guard
// guard parseable values before converting
def isNumeric: test("^-?[0-9]+(\\.[0-9]+)?([eE][+-]?[0-9]+)?$");
.<field> | select(isNumeric) | to_number Prevention
- Normalize locale formats (remove commas, units, whitespace) before to_number
- Treat semver/identifier strings separately — extract numeric components explicitly
- Pre-validate with test() regex filters so only parseable scalars reach to_number
When it happens
Trigger: `yq '.version | to_number'` where version is "1.2.3" (two dots), or to_number on values like "abc", "12px", "" (empty string scalars), or strings with thousands separators "1,000".
Common situations: Numeric-looking identifiers that are not valid numbers (semver strings, phone numbers, hex without 0x handling), locale-formatted numbers with commas, or whitespace/units embedded in the value.
Related errors
- cannot convert node at path %v of tag %v to number
- orderedMap: invalid yaml node
- expected to find one 'value' entry but found %v in position
- cannot multiply %v with %v
- cannot repeat string by a negative number (%v)
AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05).
Data as JSON: /api/errors/d3cfc201a25186b1.
Report an issue: GitHub.