hyperledger/fabric · error

could not parse the collection configuration

Error message

could not parse the collection configuration

What it means

getCollectionConfigFromBytes parses a JSON array of collection config entries (collectionConfigJson) into a protobuf CollectionConfigPackage. When json.Unmarshal fails because the bytes are not valid JSON or don't match the expected schema (array of objects with name, policy, requiredPeerCount, etc.), the error is wrapped as "could not parse the collection configuration". This signals the caller supplied a malformed collection configuration file or byte payload.

Source

Thrown at internal/peer/chaincode/common.go:183

// from the supplied file; the supplied file must contain a
// json-formatted array of collectionConfigJson elements
func GetCollectionConfigFromFile(ccFile string) (*pb.CollectionConfigPackage, []byte, error) {
	fileBytes, err := os.ReadFile(ccFile)
	if err != nil {
		return nil, nil, errors.Wrapf(err, "could not read file '%s'", ccFile)
	}

	return getCollectionConfigFromBytes(fileBytes)
}

// getCollectionConfigFromBytes retrieves the collection configuration
// from the supplied byte array; the byte array must contain a
// json-formatted array of collectionConfigJson elements
func getCollectionConfigFromBytes(cconfBytes []byte) (*pb.CollectionConfigPackage, []byte, error) {
	cconf := &[]collectionConfigJson{}
	err := json.Unmarshal(cconfBytes, cconf)
	if err != nil {
		return nil, nil, errors.Wrap(err, "could not parse the collection configuration")
	}

	ccarray := make([]*pb.CollectionConfig, 0, len(*cconf))
	for _, cconfitem := range *cconf {
		p, err := policydsl.FromString(cconfitem.Policy)
		if err != nil {
			return nil, nil, errors.WithMessagef(err, "invalid policy %s", cconfitem.Policy)
		}

		cpc := &pb.CollectionPolicyConfig{
			Payload: &pb.CollectionPolicyConfig_SignaturePolicy{
				SignaturePolicy: p,
			},
		}

		var ep *pb.ApplicationPolicy
		if cconfitem.EndorsementPolicy != nil {
			signaturePolicy := cconfitem.EndorsementPolicy.SignaturePolicy

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Validate the collection config file with 'jq . collections.json' to confirm it is a JSON array of objects
  2. Ensure the file matches the collectionConfigJson schema: an array with objects containing name, policy, requiredPeerCount, maxPeerCount, and optional blockToLive/memberOnlyReads fields
  3. Check file encoding (UTF-8, no BOM) and remove trailing commas or comments
  4. Regenerate the config from the Fabric sample templates to match your Fabric version's schema

Example fix

// before (invalid: object, not array)
{"name":"col1","policy":"OR('Org1MSP.member')"}
// after
[{"name":"col1","policy":"OR('Org1MSP.member')","requiredPeerCount":1,"maxPeerCount":2,"blockToLive":0}]
Defensive patterns

Strategy: validation

Validate before calling

cfg, err := os.ReadFile("collections.json")
if err != nil { return err }
var entries []map[string]any
if err := json.Unmarshal(cfg, &entries); err != nil { return fmt.Errorf("collections.json is not valid JSON array: %w", err) }
for _, e := range entries {
	if _, ok := e["name"].(string); !ok { return errors.New("collection entry missing name") }
	if _, ok := e["policy"].(string); !ok { return errors.New("collection entry missing policy") }
}

Type guard

func isValidCollectionConfig(b []byte) bool {
	var c []map[string]any
	return json.Unmarshal(b, &c) == nil && len(c) > 0
}

Try / catch

ccp, _, err := getCollectionConfigFromBytes(cconfBytes)
if err != nil {
	var syntaxErr *json.SyntaxError
	if errors.As(err, &syntaxErr) { log.Fatalf("malformed JSON at offset %d: %v", syntaxErr.Offset, syntaxErr) }
	return fmt.Errorf("check collection config file: %w", err)
}

Prevention

When it happens

Trigger: Calling GetCollectionConfigFromFile with a file containing invalid JSON, or passing bytes that are a single JSON object instead of an array, or entries missing/misspelling required fields (e.g. "name", "policy"), so json.Unmarshal into []collectionConfigJson fails.

Common situations: Hand-writing private data collection JSON files for chaincode endorsement policies (typos, trailing commas, YAML pasted instead of JSON), stale config from a prior Fabric version with a different schema, or passing a collection config meant for another format.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/3e77405d2768a672. Report an issue: GitHub.