hyperledger/fabric · error

failed to marshal output

Error message

failed to marshal output

What it means

After successfully unmarshaling the response payload, printResponseAsJSON re-serializes the message with json.MarshalIndent for human-readable output. If JSON marshaling fails (rare for protobuf-backed messages), the error is wrapped as "failed to marshal output".

Source

Thrown at internal/peer/lifecycle/chaincode/common.go:130

	if collectionsConfigFile != "" {
		var err error
		ccp, _, err = chaincode.GetCollectionConfigFromFile(collectionsConfigFile)
		if err != nil {
			return nil, errors.WithMessagef(err, "invalid collection configuration in file %s", collectionsConfigFile)
		}
	}
	return ccp, nil
}

func printResponseAsJSON(proposalResponse *pb.ProposalResponse, msg proto.Message, out io.Writer) error {
	err := proto.Unmarshal(proposalResponse.Response.Payload, msg)
	if err != nil {
		return errors.Wrapf(err, "failed to unmarshal proposal response's response payload as type %T", msg)
	}

	bytes, err := json.MarshalIndent(msg, "", "\t")
	if err != nil {
		return errors.Wrap(err, "failed to marshal output")
	}

	fmt.Fprintf(out, "%s\n", string(bytes))

	return nil
}

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Inspect the wrapped json error to find the offending field/type
  2. Use protojson (jsonpb) marshaling for protobuf messages in forked code
  3. Upgrade/patch the fork to use standard lifecycle result messages

Example fix

// before
bytes, err := json.MarshalIndent(msg, "", "\t")
// after
bytes, err := protojson.MarshalOptions{Multiline: true, Indent: "\t"}.Marshal(msg)
Defensive patterns

Strategy: try-catch

Try / catch

if err := printResponseAsJSON(resp, msg, out); err != nil {
    if strings.Contains(err.Error(), "failed to marshal output") {
        // fall back to printing the raw payload
    }
    return err
}

Prevention

When it happens

Trigger: printResponseAsJSON receives a msg whose JSON conversion fails — e.g. a message containing unsupported/invalid values (NaN-like fields, custom types without JSON support).

Common situations: Extremely rare with stock fabric messages; can appear in forks that pass non-standard proto messages or when jsonpb marshaling options conflict.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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