{"record":{"id":"072085890acd3549","repo":"mikefarah/yq","slug":"cannot-convert-node-at-path-v-of-tag-v-to-number","errorCode":null,"errorMessage":"cannot convert node at path %v of tag %v to number","messagePattern":"cannot convert node at path (.+?) of tag (.+?) to number","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_to_number.go","lineNumber":33,"sourceCode":"\t// try float\n\t_, floatErr := strconv.ParseFloat(value, 64)\n\n\tif floatErr == nil {\n\t\treturn \"!!float\", true\n\t}\n\treturn \"\", false\n\n}\n\nfunc toNumberOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {\n\tlog.Debugf(\"ToNumberOperator\")\n\n\tvar results = list.New()\n\n\tfor el := context.MatchingNodes.Front(); el != nil; el = el.Next() {\n\t\tcandidate := el.Value.(*CandidateNode)\n\t\tif candidate.Kind != ScalarNode {\n\t\t\treturn Context{}, fmt.Errorf(\"cannot convert node at path %v of tag %v to number\", candidate.GetNicePath(), candidate.Tag)\n\t\t}\n\n\t\tif candidate.Tag == \"!!int\" || candidate.Tag == \"!!float\" {\n\t\t\t// it already is a number!\n\t\t\tresults.PushBack(candidate)\n\t\t} else {\n\t\t\ttag, converted := tryConvertToNumber(candidate.Value)\n\t\t\tif converted {\n\t\t\t\tresult := candidate.CreateReplacement(ScalarNode, tag, candidate.Value)\n\t\t\t\tresults.PushBack(result)\n\t\t\t} else {\n\t\t\t\treturn Context{}, fmt.Errorf(\"cannot convert node value [%v] at path %v of tag %v to number\", candidate.Value, candidate.GetNicePath(), candidate.Tag)\n\t\t\t}\n\n\t\t}\n\t}\n\n\treturn context.ChildContext(results), nil","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_to_number.go#L15-L51","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Narrow the expression to scalar fields only, e.g. `.data.count | to_number` instead of `.data | to_number`","Pre-filter non-scalars: `[.[] | select(tag == \"!!str\" or tag == \"!!int\" or tag == \"!!float\")] | map(to_number)`","Convert compound nodes to a scalar first (e.g. length, tostring) before calling to_number"],"exampleFix":"// before: applies to maps too\n.items[] | to_number\n// after: target the scalar field\n.items[].count | to_number","handlingStrategy":"type-guard","validationCode":"// only convert scalars\n.data.count | select(kind == \"scalar\") | to_number","typeGuard":"// expression-level guard\ndef isScalar: kind == \"scalar\";\n.<field> | select(isScalar) | to_number","tryCatchPattern":null,"preventionTips":["Narrow expressions to scalar fields, not objects or arrays","Pre-filter with select(tag == ...) or select(kind == \"scalar\") before to_number","Inspect node kinds with yq '<expr> | kind' when unsure"],"tags":["yq","to-number","type-error","yaml"],"backgroundTag":"type-conversion-failed","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"}