kubernetes/kops · error
marshaling to json for %v: %w
Error message
marshaling to json for %v: %w
What it means
After cleaning each item, dumpGVRNamespaces serializes the list with resourceList.MarshalJSON(). If the unstructured list cannot be marshaled to JSON (invalid runtime data, unsupported types), the error is wrapped as "marshaling to json for %v: %w". This indicates data corruption or an unserializable object inside the list returned by the API.
Source
Thrown at pkg/dump/resourcedumper.go:241
if err != nil {
return err
}
o.SetManagedFields(nil)
if err := maskObject(obj); err != nil {
return err
}
return nil
})
if err != nil {
results <- resourceDumpResult{
err: fmt.Errorf("creating accessor for %v: %w", job, err),
}
continue
}
contents, err := resourceList.MarshalJSON()
if err != nil {
results <- resourceDumpResult{
err: fmt.Errorf("marshaling to json for %v: %w", job, err),
}
continue
}
switch d.output {
case "yaml":
contents, err = yaml.JSONToYAML(contents)
if err != nil {
results <- resourceDumpResult{
err: fmt.Errorf("marshaling to yaml for %v: %w", job, err),
}
continue
}
}
_, err = resFile.Write(contents)
if err != nil {
results <- resourceDumpResult{
err: fmt.Errorf("encoding resources for %v: %w", job, err),View on GitHub (pinned to 4c8573c808)
Solutions
- Inspect the offending resource (given by the %v job in the error) with `kubectl get -o json` to find the malformed field.
- Fix or remove the malformed CRD instance / aggregated API response.
- Upgrade kOps and k8s.io/apimachinery — many marshal edge cases were fixed upstream.
- Work around by dumping with a different output format or excluding that resource, then file an issue.
Example fix
// before
contents, err := resourceList.MarshalJSON()
// after (log which item fails)
contents, err := resourceList.MarshalJSON()
if err != nil {
klog.Warningf("skipping %v: %v", job, err)
continue
} Defensive patterns
Strategy: try-catch
Validate before calling
// Sanity-check the resource is JSON-serializable before dumping
list, err := dynamicClient.Resource(gvr).Namespace(ns).List(ctx, metav1.ListOptions{})
if err == nil {
if _, err := list.MarshalJSON(); err != nil {
log.Printf("skipping %v: not JSON-serializable: %v", gvr, err)
}
} Try / catch
if err := dump(); err != nil {
if strings.Contains(err.Error(), "marshaling to json for") {
// fall back to raw bytes from the API (kubectl get --raw) or skip the resource
}
return err
} Prevention
- Validate custom resource instances (kubeconform/kubectl apply --dry-run) so malformed data never reaches dumps.
- Keep sigs.k8s.io/yaml and apimachinery versions current.
- Watch for non-string map keys or NaN values in CRs served by aggregated apiservers.
- Test dumps against staging clusters with representative CRD data.
When it happens
Trigger: resourceList.MarshalJSON() fails because the list returned by the dynamic client contains items with data the JSON encoder cannot handle — e.g. invalid numeric values (NaN), non-string keys in unexpected positions, or corrupted runtime.Unstructured content.
Common situations: A CRD or aggregated API server emitting malformed JSON that the client converted to unstructured data poorly; very unusual field types in custom resources; a Go-side encoding/json failure on objects with unexpected shapes (maps with non-string keys).
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- error building annotation patch: %v
- building node patch: %w
- error building node patch: %v
- error marshaling into JSON: %v
- error parsing version spec %q
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/6a778017b76d5ddd.
Report an issue: GitHub.