{"record":{"id":"10d443efce81eec0","repo":"hashicorp/terraform","slug":"failed-to-marshal-outputs-to-json-w","errorCode":null,"errorMessage":"failed to marshal outputs to json: %w","messagePattern":"failed to marshal outputs to json: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloud/state.go","lineNumber":228,"sourceCode":"\tif schemas != nil {\n\t\tjsonState, err = jsonstate.Marshal(f, schemas)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\tstateFile, err := statefile.Read(bytes.NewReader(buf.Bytes()))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to read state: %w\", err)\n\t}\n\n\tov, err := jsonstate.MarshalOutputs(stateFile.State.RootOutputValues)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to translate outputs: %w\", err)\n\t}\n\tjsonStateOutputs, err := json.Marshal(ov)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to marshal outputs to json: %w\", err)\n\t}\n\n\terr = s.uploadState(s.lineage, s.serial, s.forcePush, buf.Bytes(), jsonState, jsonStateOutputs)\n\tif err != nil {\n\t\ts.stateUploadErr = true\n\t\treturn fmt.Errorf(\"error uploading state: %w\", err)\n\t}\n\t// After we've successfully persisted, what we just wrote is our new\n\t// reference state until someone calls RefreshState again.\n\t// We've potentially overwritten (via force) the state, lineage\n\t// and / or serial (and serial was incremented) so we copy over all\n\t// three fields so everything matches the new state and a subsequent\n\t// operation would correctly detect no changes to the lineage, serial or state.\n\ts.readState = s.state.DeepCopy()\n\ts.readLineage = s.lineage\n\ts.readSerial = s.serial\n\n\treturn nil","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/cloud/state.go#L210-L246","documentation":"Thrown during PersistState after Terraform marshals root output values: json.Marshal(ov) fails on the map returned by jsonstate.MarshalOutputs. It means an internal serialization of the state's root outputs could not be JSON-encoded for the json-state-outputs field of the state-version upload. This is an internal/serialization failure rather than a normal configuration problem.","triggerScenarios":"PersistState(schemas) is called and one of the root output values produced by jsonstate.MarshalOutputs is not JSON-encodable. In practice this requires a malformed or internally-inconsistent output value object, since well-formed outputs always serialize.","commonSituations":"Almost always an internal Terraform bug or a corrupted state object, e.g. after a bad state upgrade or a go-cty value that does not round-trip. Normal configs do not trigger it.","solutions":["Re-run terraform refresh / terraform apply; if the state is internally valid the re-marshal usually succeeds.","Inspect root output declarations for exotic or unsupported value shapes and simplify them.","If reproducible, dump the offending output values and file a Terraform issue with the state version.","As a workaround, remove or simplify the suspect output block and re-apply to rewrite state."],"exampleFix":"// before: an output holding a non-encodable/internal value\noutput \"x\" { value = local.something_odd }\n\n// after: keep outputs to plain, encodable types\noutput \"x\" { value = var.simple_string }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := state.PersistState(schemas); err != nil {\n    // PersistState already wraps the cause with %w\n    var jsonErr *json.UnsupportedTypeError\n    if errors.As(err, &jsonErr) {\n        // root output contains a non-encodable value type; simplify outputs and retry\n        return fmt.Errorf(\"unencodable output value, simplify outputs: %w\", err)\n    }\n    return err\n}","preventionTips":["Keep root outputs to plain, JSON-encodable types","Avoid custom/internal values in outputs","Re-run on transient serialization hiccups before debugging"],"tags":["serialization","state","outputs","json","persist"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}