{"record":{"id":"f71b73be199105b8","repo":"hyperledger/fabric","slug":"json-format-is-not-valid","errorCode":null,"errorMessage":"JSON format is not valid","messagePattern":"JSON format is not valid","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/ledger/kvledger/txmgmt/statedb/statecouchdb/couchdb.go","lineNumber":1151,"sourceCode":"\t\t\tresults = append(results, addIndexResult)\n\t\t}\n\n\t}\n\n\tcouchdbLogger.Debugf(\"[%s] Exiting ListIndex()\", dbclient.dbName)\n\n\treturn results, nil\n}\n\n// createIndex method provides a function creating an index\nfunc (dbclient *couchDatabase) createIndex(indexdefinition string) (*createIndexResponse, error) {\n\tdbName := dbclient.dbName\n\n\tcouchdbLogger.Debugf(\"[%s] Entering CreateIndex()  indexdefinition=%s\", dbName, indexdefinition)\n\n\t// Test to see if this is a valid JSON\n\tif !isJSON(indexdefinition) {\n\t\treturn nil, errors.New(\"JSON format is not valid\")\n\t}\n\n\tindexURL, err := url.Parse(dbclient.couchInstance.url())\n\tif err != nil {\n\t\tcouchdbLogger.Errorf(\"URL parse error: %s\", err)\n\t\treturn nil, errors.Wrapf(err, \"error parsing CouchDB URL: %s\", dbclient.couchInstance.url())\n\t}\n\n\t// get the number of retries\n\tmaxRetries := dbclient.couchInstance.conf.MaxRetries\n\n\tresp, _, err := dbclient.handleRequest(http.MethodPost, \"CreateIndex\", indexURL, []byte(indexdefinition), \"\", \"\", maxRetries, true, nil, \"_index\")\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer closeResponseBody(resp)\n\n\tif resp == nil {","sourceCodeStart":1133,"sourceCodeEnd":1169,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/ledger/kvledger/txmgmt/statedb/statecouchdb/couchdb.go#L1133-L1169","documentation":"CreateIndex validates the supplied index definition string with isJSON() before sending it to CouchDB. If the definition is not syntactically valid JSON, this plain error is returned and no HTTP call is made. It is a client-side input validation failure, not a CouchDB response problem.","triggerScenarios":"Calling CreateIndex (or the richer IndexDescription-based API that serializes to JSON) with a hand-written index definition string containing syntax errors: trailing commas, single quotes, comments, unquoted keys, or an empty/whitespace string.","commonSituations":"Hand-editing chaincode index JSON copied from docs; building the definition with string concatenation instead of marshalling a struct; template/variable substitution producing invalid JSON; forgetting to escape quotes in embedded definitions.","solutions":["Validate the index definition with a JSON linter or json.Valid() before calling CreateIndex","Build the definition by marshalling a Go map/struct (json.Marshal) instead of writing raw JSON strings","Fix syntax errors: trailing commas, single quotes, unquoted field names, comments","Ensure the definition follows the required shape {\"index\":{...},\"ddoc\":\"...\",\"name\":\"...\",\"type\":\"json\"}"],"exampleFix":"// before\nindexDef := `{\"index\":{\"fields\":[\"docType\",\"id\"],}, \"ddoc\":\"idx\", \"name\":\"idx\", \"type\":\"json\"}` // trailing comma\nclient.CreateIndex(indexDef)\n// after\nindexDef := `{\"index\":{\"fields\":[\"docType\",\"id\"]},\"ddoc\":\"idx\",\"name\":\"idx\",\"type\":\"json\"}`\nif !json.Valid([]byte(indexDef)) { return errors.New(\"invalid index definition\") }\nclient.CreateIndex(indexDef)","handlingStrategy":"validation","validationCode":"func validIndexDef(def string) bool {\n    if !json.Valid([]byte(def)) { return false }\n    var m map[string]interface{}\n    if json.Unmarshal([]byte(def), &m) != nil { return false }\n    _, hasIndex := m[\"index\"]\n    return hasIndex\n}\n// call only if validIndexDef(indexDef)","typeGuard":null,"tryCatchPattern":"if !json.Valid([]byte(indexDef)) {\n    return errors.New(\"index definition is not valid JSON\")\n}\nerr := client.CreateIndex(indexDef)","preventionTips":["Build index definitions with json.Marshal of a struct/map, never string concatenation","Run a JSON linter on chaincode META-INF/statedb/couchdb/index files at build time","Test index deployment in CI against a scratch CouchDB"],"tags":["couchdb","json","validation","index"],"backgroundTag":"invalid-json-input","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"}