{"record":{"id":"c0b9024d8bc866fe","repo":"kubernetes/kops","slug":"unable-to-marshal-json-v-c0b902","errorCode":null,"errorMessage":"unable to marshal JSON: %v","messagePattern":"unable to marshal JSON: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/get_instances.go","lineNumber":171,"sourceCode":"\t\tcg.AdjustNeedUpdate()\n\t}\n\n\tswitch options.Output {\n\tcase OutputTable:\n\t\treturn instanceOutputTable(cloudInstances, out)\n\tcase OutputYaml:\n\t\ty, err := yaml.Marshal(asRenderable(cloudInstances))\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to marshal YAML: %v\", err)\n\t\t}\n\t\tif _, err := out.Write(y); err != nil {\n\t\t\treturn fmt.Errorf(\"error writing to output: %v\", err)\n\t\t}\n\t\treturn nil\n\tcase OutputJSON:\n\t\tj, err := json.Marshal(asRenderable(cloudInstances))\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to marshal JSON: %v\", err)\n\t\t}\n\t\tif _, err := out.Write(j); err != nil {\n\t\t\treturn fmt.Errorf(\"error writing to output: %v\", err)\n\t\t}\n\t\treturn nil\n\tdefault:\n\t\treturn fmt.Errorf(\"unsupported output format: %q\", options.Output)\n\t}\n}\n\nfunc instanceOutputTable(instances []*cloudinstances.CloudInstance, out io.Writer) error {\n\tfmt.Println(\"\")\n\tt := &tables.Table{}\n\tt.AddColumn(\"ID\", func(i *cloudinstances.CloudInstance) string {\n\t\treturn i.ID\n\t})\n\tt.AddColumn(\"NODE-NAME\", func(i *cloudinstances.CloudInstance) string {\n\t\tnode := i.Node","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/get_instances.go#L153-L189","documentation":"For `kops get instances --output json`, the renderable instance list is serialized with encoding/json.Marshal; a failure is wrapped as \"unable to marshal JSON: %v\". json.Marshal fails only on values it cannot encode (unsupported types like channels/funcs, or NaN/Inf floats), which is unusual here since renderableCloudInstance contains only plain string/slice fields.","triggerScenarios":"`kops get instances -o json` where json.Marshal(asRenderable(cloudInstances)) at get_instances.go:169-171 returns an error — e.g. instance metadata injected NaN/Inf or invalid values into a numeric field of the renderable struct.","commonSituations":"Custom patches or forks that add numeric fields populated with NaN from cloud provider metrics; invalid UTF-8 strings from instance tags; broken vendored cloudinstances types.","solutions":["Read the wrapped message — it names the offending type/value (e.g. 'json: unsupported value: NaN') and the failing field","Use `kops get instances` (table) to find the instance producing the bad value and inspect it in your cloud console","Sanitize upstream metadata (instance tags/machine types) that injects NaN, Inf, or invalid UTF-8","Upgrade to a stock kops release to rule out a local fork/patch adding non-encodable fields"],"exampleFix":"// before\nMachineType: ci.MachineType,  // may carry invalid data from provider\n// after: sanitize strings before marshaling\nMachineType: strings.ToValidUTF8(ci.MachineType, \"\\uFFFD\"),","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"func jsonSafe(instances []*cloudinstances.CloudInstance) bool {\n    _, err := json.Marshal(asRenderable(instances))\n    return err == nil\n}","tryCatchPattern":"j, err := json.Marshal(asRenderable(cloudInstances))\nif err != nil {\n    return fmt.Errorf(\"unable to marshal JSON: %w\", err)\n}","preventionTips":["Keep renderable structs limited to string/int fields the stdlib can always encode","Sanitize any numeric data derived from provider metrics (reject NaN/Inf)","Run `kops get instances -o json` in CI to catch encoding regressions early","Read the wrapped error message — it names the exact unsupported value and field"],"tags":["json","serialization","cli-output"],"backgroundTag":"json-marshal-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}