{"record":{"id":"2fec1b54fb9dc4e1","repo":"hyperledger/fabric","slug":"chaincode-argument-error","errorCode":null,"errorMessage":"chaincode argument error","messagePattern":"chaincode argument error","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/peer/chaincode/common.go","lineNumber":47,"sourceCode":"\t\"github.com/pkg/errors\"\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/viper\"\n\t\"google.golang.org/protobuf/proto\"\n)\n\n// getChaincodeSpec get chaincode spec from the cli cmd parameters\nfunc getChaincodeSpec(cmd *cobra.Command) (*pb.ChaincodeSpec, error) {\n\tspec := &pb.ChaincodeSpec{}\n\tif err := checkChaincodeCmdParams(cmd); err != nil {\n\t\t// unset usage silence because it's a command line usage error\n\t\tcmd.SilenceUsage = false\n\t\treturn spec, err\n\t}\n\n\t// Build the spec\n\tinput := chaincodeInput{}\n\tif err := json.Unmarshal([]byte(chaincodeCtorJSON), &input); err != nil {\n\t\treturn spec, errors.Wrap(err, \"chaincode argument error\")\n\t}\n\tinput.IsInit = isInit\n\n\tchaincodeLang = strings.ToUpper(chaincodeLang)\n\tspec = &pb.ChaincodeSpec{\n\t\tType:        pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[chaincodeLang]),\n\t\tChaincodeId: &pb.ChaincodeID{Path: chaincodePath, Name: chaincodeName, Version: chaincodeVersion},\n\t\tInput:       &input.ChaincodeInput,\n\t}\n\treturn spec, nil\n}\n\n// chaincodeInput is wrapper around the proto defined ChaincodeInput message that\n// is decorated with a custom JSON unmarshaller.\ntype chaincodeInput struct {\n\tpb.ChaincodeInput\n}\n","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/internal/peer/chaincode/common.go#L29-L65","documentation":"Wrapping error in getChaincodeSpec: the -ctor (constructor) JSON supplied on the command line could not be unmarshaled into a chaincodeInput. The user's --ctor argument is malformed JSON or does not match the expected shape {\"Args\":[...]} / {\"Function\":\"f\",\"Args\":[...]}; the wrapped json error gives the exact syntax problem.","triggerScenarios":"Calling peer chaincode invoke/query with a --ctor string that is not valid JSON, e.g. mismatched braces, single quotes instead of double quotes, or missing the Args/function structure.","commonSituations":"Shell quoting stripping double quotes around Args arrays; copy-pasted function names without the '{\"function\":\"x\",\"Args\":[...]}' wrapper; using old '{\"Args\":[...]}' style where init is required.","solutions":["Validate the --ctor JSON, e.g. '{\"function\":\"Invoke\",\"Args\":[\"a\",\"b\",\"1\"]}'","Use single quotes around the whole JSON in bash so inner double quotes survive","Use peer chaincode invoke --peerAddresses ... with --ctor written via a file or echo test to confirm it parses with jq","If using go-based chaincodeArgs helper (peer CLI code), prefer the structured API instead of hand-written JSON"],"exampleFix":"// before\npeer chaincode invoke -C mychannel -n mycc --ctor '{Args:[\"put\",\"a\",\"1\"]}'\n// after\npeer chaincode invoke -C mychannel -n mycc --ctor '{\"Args\":[\"put\",\"a\",\"1\"]}'","handlingStrategy":"validation","validationCode":"// Validate ctor JSON before invoking the CLI\nconst ctor = JSON.parse(process.argv[2]);\nif (typeof ctor !== 'object' || !(Array.isArray(ctor.Args) || typeof ctor.function === 'string')) {\n  throw new Error('ctor must be {\"function\":... ,\"Args\":[...] }');\n}","typeGuard":null,"tryCatchPattern":"try {\n  JSON.parse(ctorJSON);\n} catch (e) {\n  console.error('invalid --ctor JSON, quote inner strings with double quotes inside single quotes:', e.message);\n  process.exit(1);\n}","preventionTips":["Always wrap --ctor in single quotes in bash","Test JSON with jq before passing to peer CLI","Prefer generated scripts or SDKs over hand-typed JSON","Remember --ctor must be valid JSON, not JavaScript object literal syntax"],"tags":["cli","json","chaincode"],"backgroundTag":"invalid-json-arguments","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"}