Hyperledger Fabric: peer chaincode invoke: pass JSON object to chaincode function?

Viewed 2023

I am able to call the chaincode functionality by peer chaincode invoke as:

`peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"function":"initLedger","Args":["one", "two"]}`

But I want to pass javascript object as argument like:

`peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"function":"initLedger","Args":[ {property1:"one"}, {property2:"two"}]}`  

but this fails with invalid character 'p' looking for beginning of object key string. I have tried few other permutations but all without success.

the documentation says about -c as:
-c, --ctor string Constructor message for the chaincode in JSON format (default "{}")

Can come one tell correct syntax to pass the object.

EDIT 1: For the sake of completeness following

peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"function":"initLedger","Args": [{"property1":"one", "property2":"two"} ]}'

returns Error: Chaincode argument error: json: cannot unmarshal array into Go struct field strArgs.Args of type string

1 Answers

I think you might be using an old format of the invoke command, so I'm suggesting a new format like this including your stringified JSON object:

peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"Args":["initLedger","{\"property1\":\"one\",\"property2\":\"two\"}"]}'

But if you need to use your format then perhaps:

peer chaincode invoke -o localhost:7050 -C mychannel -n pmc -c '{"function":"initLedger", "Args": ["{\"property1\":\"one\",\"property2\":\"two\"}"]}'

Related