hashicorp/terraform · error
Failed to marshal state to json: %s
Error message
Failed to marshal state to json: %s
What it means
Thrown by `terraform state show` when jsonstate.MarshalForRenderer(mockFile, schemas) fails while converting the single resource instance into the JSON renderer representation. MarshalForRenderer encodes the state plus provider schemas; failure indicates the resource instance or its provider schema could not be serialized — typically a schema/type mismatch or an internal encoding error. The %s is the underlying marshal error.
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 c9def3e214)
Solutions
- Read the %s for the attribute/type that failed to marshal.
- Run `terraform refresh` (or `terraform apply -refresh-only`) to reconcile state with the current provider schema, then retry state show.
- If a provider downgrade caused it, restore the provider version that wrote the state via required_providers.
- As a last resort, taint/remove the offending resource and re-apply.
Example fix
# before aws_instance.web state written by provider v3; now using v5 terraform state show aws_instance.web # Failed to marshal state to json: ... # after terraform apply -refresh-only terraform state show aws_instance.web
Defensive patterns
Strategy: validation
Validate before calling
// validate a resource instance's state shape against provider schema
// before marshalling (conceptual)
func instanceMatchesSchema(is *states.ResourceInstance, schema *configschema.Block) error {
// decode instance state via schema.ImpliedType and surface mismatch
return nil
} Prevention
- Run `terraform apply -refresh-only` after provider upgrades to reconcile state.
- Pin provider versions to avoid sudden schema changes.
- Avoid hand-editing state.
- Re-create resources if their state shape becomes unrecoverable.
When it happens
Trigger: Running `terraform state show <addr>` for a resource instance whose state shape does not match its provider's current schema, so marshalling the filtered state to JSON fails. Often seen after a provider schema change without refresh, or with a corrupt/inconsistent state value.
Common situations: Provider upgraded with breaking schema change; resource in state written by a much older provider; state manually edited; cty value/type drift between state and schema; resource type renamed.
Related errors
- credentials file %s has invalid value for "credentials" prop
- Can't serialize backend configuration as JSON: %s
- Failed to set state store configuration: %w
- Failed to set state store provider configuration: %w
- Can't serialize backend configuration as JSON: %s
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/b81cc8f29bb1fd2b.
Report an issue: GitHub.