{"record":{"id":"15ee4f5e52e84eb9","repo":"hyperledger/fabric","slug":"error-encoding-json-data","errorCode":null,"errorMessage":"error encoding json data","messagePattern":"error encoding json data","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/ledger/kvledger/txmgmt/statedb/statecouchdb/couchdb.go","lineNumber":1786,"sourceCode":"// encodePathElement uses Golang for url path encoding, additionally:\n// '/' is replaced by %2F, otherwise path encoding will treat as path separator and ignore it\n// '+' is replaced by %2B, otherwise path encoding will ignore it, while CouchDB will unencode the plus as a space\n// Note that all other URL special characters have been tested successfully without need for special handling\nfunc encodePathElement(str string) string {\n\tu := &url.URL{}\n\tu.Path = str\n\tencodedStr := u.EscapedPath() // url encode using golang url path encoding rules\n\tencodedStr = strings.Replace(encodedStr, \"/\", \"%2F\", -1)\n\tencodedStr = strings.Replace(encodedStr, \"+\", \"%2B\", -1)\n\n\treturn encodedStr\n}\n\nfunc encodeForJSON(str string) (string, error) {\n\tbuf := &bytes.Buffer{}\n\tencoder := json.NewEncoder(buf)\n\tif err := encoder.Encode(str); err != nil {\n\t\treturn \"\", errors.Wrap(err, \"error encoding json data\")\n\t}\n\t// Encode adds double quotes to string and terminates with \\n - stripping them as bytes as they are all ascii(0-127)\n\tbuffer := buf.Bytes()\n\treturn string(buffer[1 : len(buffer)-2]), nil\n}\n\n// printDocumentIds is a convenience method to print readable log entries for arrays of pointers\n// to couch document IDs\nfunc printDocumentIds(documentPointers []*couchDoc) (string, error) {\n\tdocumentIds := []string{}\n\n\tfor _, documentPointer := range documentPointers {\n\t\tdocMetadata := &docMetadata{}\n\t\terr := json.Unmarshal(documentPointer.jsonValue, &docMetadata)\n\t\tif err != nil {\n\t\t\treturn \"\", errors.Wrap(err, \"error unmarshalling json data\")\n\t\t}\n\t\tdocumentIds = append(documentIds, docMetadata.ID)","sourceCodeStart":1768,"sourceCodeEnd":1804,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/ledger/kvledger/txmgmt/statedb/statecouchdb/couchdb.go#L1768-L1804","documentation":"encodeForJSON wraps the Go standard library json.Encoder failure when encoding a string to JSON. It occurs when marshalling a string value into the JSON buffer fails, which is extremely rare because encoding a Go string to JSON practically never errors. The error is wrapped with the message \"error encoding json data\".","triggerScenarios":"json.NewEncoder(buf).Encode(str) returns an error inside encodeForJSON; in practice only triggered by a failure writing to the underlying bytes.Buffer (e.g. memory allocation failure).","commonSituations":"Almost never seen in normal operation; would appear only under severe memory pressure or an OOM condition in the peer process.","solutions":["Retry the operation; this is a transient/environmental failure.","Check peer memory usage and host resources if it recurs.","If reproducible, upgrade Go/Fabric version as it indicates an internal bug."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err != nil {\n    if strings.Contains(err.Error(), \"error encoding json data\") {\n        // transient/environmental; retry once after freeing resources\n    }\n    return err\n}","preventionTips":["Ensure the peer host has adequate memory.","Monitor for OOM conditions in container logs.","Treat recurrence as an internal bug and report/upgrade."],"tags":["couchdb","json","encoding"],"backgroundTag":"json-encode-failed","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}