{"record":{"id":"b81cc8f29bb1fd2b","repo":"hashicorp/terraform","slug":"failed-to-marshal-state-to-json-s","errorCode":null,"errorMessage":"Failed to marshal state to json: %s","messagePattern":"Failed to marshal state to json: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/state_show.go","lineNumber":151,"sourceCode":"\n\t// check if the resource has a configured provider, otherwise this will use the default provider\n\trs := state.Resource(addr.ContainingResource())\n\tabsPc := addrs.AbsProviderConfig{\n\t\tProvider: rs.ProviderConfig.Provider,\n\t\tAlias:    rs.ProviderConfig.Alias,\n\t\tModule:   addrs.RootModule,\n\t}\n\tsingleInstance := states.NewState()\n\tsingleInstance.EnsureModule(addr.Module).SetResourceInstanceCurrent(\n\t\taddr.Resource,\n\t\tis.Current,\n\t\tabsPc,\n\t)\n\n\tmockFile := statefile.New(singleInstance, \"\", 0)\n\troot, outputs, err := jsonstate.MarshalForRenderer(mockFile, schemas)\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to marshal state to json: %s\", err))\n\t\treturn view.DisplayResourceInstanceState(jsonformat.State{}, diags)\n\t}\n\n\tjstate := jsonformat.State{\n\t\tStateFormatVersion:    jsonstate.FormatVersion,\n\t\tProviderFormatVersion: jsonprovider.FormatVersion,\n\t\tRootModule:            root,\n\t\tRootModuleOutputs:     outputs,\n\t\tProviderSchemas:       jsonprovider.MarshalForRenderer(schemas),\n\t}\n\n\treturn view.DisplayResourceInstanceState(jstate, diags)\n}\n\nfunc (c *StateShowCommand) Help() string {\n\thelpText := `\nUsage: terraform [global options] state show [options] ADDRESS\n","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/state_show.go#L133-L169","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\naws_instance.web state written by provider v3; now using v5\nterraform state show aws_instance.web\n# Failed to marshal state to json: ...\n\n# after\nterraform apply -refresh-only\nterraform state show aws_instance.web","handlingStrategy":"validation","validationCode":"// validate a resource instance's state shape against provider schema\n// before marshalling (conceptual)\nfunc instanceMatchesSchema(is *states.ResourceInstance, schema *configschema.Block) error {\n    // decode instance state via schema.ImpliedType and surface mismatch\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["state-show","json","schema","marshalling"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}