hashicorp/terraform · error

Failed to marshal state to json

Error message

Failed to marshal state to json: %s

What it means

Thrown by state_show.go when jsonstate.MarshalForRenderer(mockFile, schemas) fails while preparing the single-instance mock state for display. The schemas come from lr.Core.Schemas(...) and the state from the refreshed state manager. Failure means the state structure does not match the provider schemas Terraform has on hand, or the marshal hit an internal invariant violation.

Solutions

  1. Run `terraform init -upgrade` to refresh provider binaries and their schemas.
  2. Confirm `required_providers` versions still cover the resource types present in state.
  3. Run `terraform state list` and `terraform state show` for other resources to isolate which resource's schema is bad.
  4. If a state upgrade is needed, run `terraform refresh`/`terraform apply -refresh-only` with matching provider versions first.

Example fix

# before: provider downgraded, schema mismatch
 terraform state show aws_instance.web

# after
 terraform init -upgrade
 terraform state show aws_instance.web
Defensive patterns

Strategy: validation

Validate before calling

// Confirm every resource type in state has a schema.
for _, mod := range state.Modules {
    for rt := range mod.Resources {
        if _, ok := schemas.ResourceTypes[rt.String()]; !ok {
            return fmt.Errorf("no schema for %s; run terraform init -upgrade", rt)
        }
    }
}

Try / catch

root, outputs, err := jsonstate.MarshalForRenderer(mockFile, schemas)
if err != nil {
    diags = diags.Append(fmt.Errorf("Failed to marshal state to json: %s; provider schema may be stale — run terraform init -upgrade", err))
    return view.DisplayResourceInstanceState(jsonformat.State{}, diags)
}

Prevention

When it happens

Trigger: Provider schema mismatch: the resource type in state was produced by a provider version whose schema differs from the one currently cached; a state upgrade for that resource type did not run; provider plugin failed to report schema for the resource type in question.

Common situations: Downgraded provider version in required_providers; state written by a provider that has since been removed from required_providers; plugin crash during GetProviderSchema; stale schema cache after a provider version bump without re-init.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/b81cc8f29bb1fd2b. Report an issue: GitHub.

Appendix: source

Thrown at internal/command/state_show.go:151

	// check if the resource has a configured provider, otherwise this will use the default provider
	rs := state.Resource(addr.ContainingResource())
	absPc := addrs.AbsProviderConfig{
		Provider: rs.ProviderConfig.Provider,
		Alias:    rs.ProviderConfig.Alias,
		Module:   addrs.RootModule,
	}
	singleInstance := states.NewState()
	singleInstance.EnsureModule(addr.Module).SetResourceInstanceCurrent(
		addr.Resource,
		is.Current,
		absPc,
	)

	mockFile := statefile.New(singleInstance, "", 0)
	root, outputs, err := jsonstate.MarshalForRenderer(mockFile, schemas)
	if err != nil {
		diags = diags.Append(fmt.Errorf("Failed to marshal state to json: %s", err))
		return view.DisplayResourceInstanceState(jsonformat.State{}, diags)
	}

	jstate := jsonformat.State{
		StateFormatVersion:    jsonstate.FormatVersion,
		ProviderFormatVersion: jsonprovider.FormatVersion,
		RootModule:            root,
		RootModuleOutputs:     outputs,
		ProviderSchemas:       jsonprovider.MarshalForRenderer(schemas),
	}

	return view.DisplayResourceInstanceState(jstate, diags)
}

func (c *StateShowCommand) Help() string {
	helpText := `
Usage: terraform [global options] state show [options] ADDRESS

View on GitHub (pinned to d32a084675)