kubernetes/kops · error

unable to marshal JSON: %v

Error message

unable to marshal JSON: %v

What it means

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.

Source

Thrown at cmd/kops/get_instances.go:171

		cg.AdjustNeedUpdate()
	}

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

func instanceOutputTable(instances []*cloudinstances.CloudInstance, out io.Writer) error {
	fmt.Println("")
	t := &tables.Table{}
	t.AddColumn("ID", func(i *cloudinstances.CloudInstance) string {
		return i.ID
	})
	t.AddColumn("NODE-NAME", func(i *cloudinstances.CloudInstance) string {
		node := i.Node

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Read the wrapped message — it names the offending type/value (e.g. 'json: unsupported value: NaN') and the failing field
  2. Use `kops get instances` (table) to find the instance producing the bad value and inspect it in your cloud console
  3. Sanitize upstream metadata (instance tags/machine types) that injects NaN, Inf, or invalid UTF-8
  4. Upgrade to a stock kops release to rule out a local fork/patch adding non-encodable fields

Example fix

// before
MachineType: ci.MachineType,  // may carry invalid data from provider
// after: sanitize strings before marshaling
MachineType: strings.ToValidUTF8(ci.MachineType, "\uFFFD"),
Defensive patterns

Strategy: try-catch

Type guard

func jsonSafe(instances []*cloudinstances.CloudInstance) bool {
    _, err := json.Marshal(asRenderable(instances))
    return err == nil
}

Try / catch

j, err := json.Marshal(asRenderable(cloudInstances))
if err != nil {
    return fmt.Errorf("unable to marshal JSON: %w", err)
}

Prevention

When it happens

Trigger: `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.

Common situations: 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.

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/c0b9024d8bc866fe. Report an issue: GitHub.