{"record":{"id":"914abcad32c05f8e","repo":"kubernetes/kops","slug":"error-marshaling-into-json-v","errorCode":null,"errorMessage":"error marshaling into JSON: %v","messagePattern":"error marshaling into JSON: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/kubemanifest/yaml.go","lineNumber":39,"sourceCode":"\t\"fmt\"\n\n\t\"k8s.io/apimachinery/pkg/runtime\"\n\t\"k8s.io/klog/v2\"\n\t\"sigs.k8s.io/yaml\"\n)\n\n// KubeObjectToApplyYAML returns the kubernetes object converted to YAML, with \"noisy\" fields removed.\n//\n// We remove:\n//   - status (can't be applied, shouldn't be specified)\n//   - metadata.creationTimestamp (can't be applied, shouldn't be specified)\nfunc KubeObjectToApplyYAML(data runtime.Object) (string, error) {\n\t// This logic is inlined sigs.k8s.io/yaml.Marshal, but we delete some fields in the middle.\n\n\t// Convert the object to JSON bytes\n\tj, err := json.Marshal(data)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"error marshaling into JSON: %v\", err)\n\t}\n\n\t// Convert the JSON to a map.\n\tjsonObj := make(map[string]interface{})\n\tif err := yaml.Unmarshal(j, &jsonObj); err != nil {\n\t\treturn \"\", err\n\t}\n\n\t// Remove status (can't be applied, shouldn't be specified)\n\tdelete(jsonObj, \"status\")\n\n\t// Remove metadata.creationTimestamp (can't be applied, shouldn't be specified)\n\tmetadataObj, found := jsonObj[\"metadata\"]\n\tif found {\n\t\tif metadata, ok := metadataObj.(map[string]interface{}); ok {\n\t\t\tdelete(metadata, \"creationTimestamp\")\n\t\t} else {\n\t\t\tklog.Warningf(\"unexpected type for object metadata: %T\", metadataObj)","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/kubemanifest/yaml.go#L21-L57","documentation":"KubeObjectToApplyYAML in pkg/kubemanifest/yaml.go inlines sigs.k8s.io/yaml.Marshal: it first json.Marshal's a runtime.Object (a Kubernetes API object) before converting to a YAML map and deleting some fields. This error fires when the Go standard json.Marshal fails on the runtime.Object.","triggerScenarios":"Calling KubeObjectToApplyYAML with an object containing fields json.Marshal cannot encode: invalid types (chan, func, NaN/Inf floats), or a runtime.Object whose underlying Go value fails marshaling; also a nil or malformed typed object.","commonSituations":"Passing a partially-initialized k8s API struct with unsupported values, or a custom runtime.Object whose marshaling logic errors; objects built programmatically with bad values (e.g. NaN in resource quantities stored as float).","solutions":["Validate the runtime.Object with json.Marshal in a test to find the offending field","Fix or zero out the non-serializable field values before conversion","Ensure the object is a properly formed typed k8s API object (e.g. *corev1.Pod) rather than a wrapper/unregistered type"],"exampleFix":"// before: NaN quantity\nresources.Limits[\"cpu\"] = math.NaN()\nobj.KubeObjectToApplyYAML(pod)\n// after\nresources.Limits[\"cpu\"] = resource.MustParse(\"500m\")\nobj.KubeObjectToApplyYAML(pod)","handlingStrategy":"try-catch","validationCode":"if _, err := json.Marshal(data); err != nil {\n    // object is not JSON-serializable; fix fields before calling KubeObjectToApplyYAML\n}","typeGuard":"func isJSONSafe(v interface{}) bool {\n    var hasBad func(interface{}) bool\n    hasBad = func(x interface{}) bool {\n        switch x.(type) {\n        case chan interface{}, func():\n            return true\n        }\n        return false\n    }\n    return !hasBad(v)\n}","tryCatchPattern":"yamlStr, err := kubemanifest.KubeObjectToApplyYAML(pod)\nif err != nil {\n    return fmt.Errorf(\"cannot render apply yaml for %T: %w\", pod, err)\n}","preventionTips":["Validate quantities/floats contain no NaN or Inf","Ensure objects are properly initialized typed k8s API structs","Test KubeObjectToApplyYAML output in CI for all generated objects"],"tags":["go","json","serialization","kubernetes"],"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"}