kubernetes/kops · error

unable to marshal JSON: %v

Error message

unable to marshal JSON: %v

What it means

This error is raised when json.Marshal(result) fails while rendering validation results in --output json mode. Like the YAML case, marshalling a fixed validation result struct should never fail; if it does, the result contains an unsupported value (func, channel, invalid UTF-8 in strings) and usually indicates a kops bug or version skew.

Source

Thrown at cmd/kops/validate_cluster.go:221

		}

		switch options.output {
		case OutputTable:
			if err := validateClusterOutputTable(result, cluster, instanceGroups, out); err != nil {
				return nil, err
			}
		case OutputYaml:
			y, err := yaml.Marshal(result)
			if err != nil {
				return nil, fmt.Errorf("unable to marshal YAML: %v", err)
			}
			if _, err := out.Write(y); err != nil {
				return nil, fmt.Errorf("error writing to output: %v", err)
			}
		case OutputJSON:
			j, err := json.Marshal(result)
			if err != nil {
				return nil, fmt.Errorf("unable to marshal JSON: %v", err)
			}
			if _, err := out.Write(j); err != nil {
				return nil, fmt.Errorf("error writing to output: %v", err)
			}
		default:
			return nil, fmt.Errorf("unknown output format: %q", options.output)
		}

		if len(result.Failures) == 0 {
			consecutive++
			if consecutive < options.count {
				klog.Infof("(will retry): cluster passed validation %d consecutive times", consecutive)
				if options.wait > 0 {
					time.Sleep(options.interval)
					continue
				} else {
					return nil, fmt.Errorf("cluster passed validation %d consecutive times", consecutive)
				}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Re-run with default table output or -o yaml to still obtain results.
  2. Verify official kops binary and matching cluster version; rebuild/upgrade if patched.
  3. File a kops bug with the printed %v cause if it reproduces with official binaries.

Example fix

// before
kops validate cluster -o json
// after
kops validate cluster -o yaml
Defensive patterns

Strategy: fallback

Try / catch

result, err := runValidateCluster(ctx, options)
if err != nil && strings.Contains(err.Error(), "unable to marshal JSON") {
	return runValidateCluster(ctx, withOutput(options, OutputTable))
}

Prevention

When it happens

Trigger: `kops validate cluster -o json` where the result struct contains a value json.Marshal cannot encode (e.g. NaN via unsupported types, a func/channel field).

Common situations: Patched or mismatched kops binary producing a result struct with unsupported field types; custom validator injection; normally not user-triggerable.

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


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/709dc78c261bb6fb. Report an issue: GitHub.