hyperledger/fabric · error
JSON format is not valid
Error message
JSON format is not valid
What it means
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.
Source
Thrown at core/ledger/kvledger/txmgmt/statedb/statecouchdb/couchdb.go:1151
results = append(results, addIndexResult)
}
}
couchdbLogger.Debugf("[%s] Exiting ListIndex()", dbclient.dbName)
return results, nil
}
// createIndex method provides a function creating an index
func (dbclient *couchDatabase) createIndex(indexdefinition string) (*createIndexResponse, error) {
dbName := dbclient.dbName
couchdbLogger.Debugf("[%s] Entering CreateIndex() indexdefinition=%s", dbName, indexdefinition)
// Test to see if this is a valid JSON
if !isJSON(indexdefinition) {
return nil, errors.New("JSON format is not valid")
}
indexURL, err := url.Parse(dbclient.couchInstance.url())
if err != nil {
couchdbLogger.Errorf("URL parse error: %s", err)
return nil, errors.Wrapf(err, "error parsing CouchDB URL: %s", dbclient.couchInstance.url())
}
// get the number of retries
maxRetries := dbclient.couchInstance.conf.MaxRetries
resp, _, err := dbclient.handleRequest(http.MethodPost, "CreateIndex", indexURL, []byte(indexdefinition), "", "", maxRetries, true, nil, "_index")
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
if resp == nil {View on GitHub (pinned to 2736b63f8f)
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"}
Example fix
// before
indexDef := `{"index":{"fields":["docType","id"],}, "ddoc":"idx", "name":"idx", "type":"json"}` // trailing comma
client.CreateIndex(indexDef)
// after
indexDef := `{"index":{"fields":["docType","id"]},"ddoc":"idx","name":"idx","type":"json"}`
if !json.Valid([]byte(indexDef)) { return errors.New("invalid index definition") }
client.CreateIndex(indexDef) Defensive patterns
Strategy: validation
Validate before calling
func validIndexDef(def string) bool {
if !json.Valid([]byte(def)) { return false }
var m map[string]interface{}
if json.Unmarshal([]byte(def), &m) != nil { return false }
_, hasIndex := m["index"]
return hasIndex
}
// call only if validIndexDef(indexDef) Try / catch
if !json.Valid([]byte(indexDef)) {
return errors.New("index definition is not valid JSON")
}
err := client.CreateIndex(indexDef) Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- too few arguments
- incorrect number of arguments
- connection.json not found in source folder: %s
- Failed opening file %s: %v
- config isn't valid
AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04).
Data as JSON: /api/errors/f71b73be199105b8.
Report an issue: GitHub.