Search code examples
blockchainhyperledger-fabricsmartcontractschaincode

Encountering the error "Error: chaincode argument error: invalid character ']' after object key"


I am trying to modify fabcar to test various data structures possible with it. I used the following command:peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n ${CC_NAME} --peerAddresses localhost:7051 --tlsRootCertFiles $PEER0_ORG1_CA -c '{"function": "createCar", "Args":"CAR101","[{Make: \"Honda\" , Model: \"City\" , Colour: \"White\"} , {Make: \"Swift\" , Model: \"Dezire\" , Colour: \"Black\"}] , {Name: \"James\" , Age: \"29\" , Gender: \"Male\"}"]}'

But am encountering the above said error. My smart contract structure is defined as:

type CarDeal struct {
    CarDet []Car `json:"cardet"`
    OwnerDet  Owner `json:"ownerdet"`
}

type Car struct {
    Make   string `json:"make"`
    Model  string `json:"model"`
    Colour string `json:"colour"`
}

type Owner struct {
    Name string `json:"name"`
    Age string `json:"age"`
    Gender string `json:"gender"`
}

The chaincode function 'createCar' is as follows:

func (s *SmartContract) CreateCar(ctx contractapi.TransactionContextInterface, carNumber string, cardet CarDeal) error {
    
    car := cardet

    carAsBytes, _ := json.Marshal(car)

    return ctx.GetStub().PutState(carNumber, carAsBytes)

}

Can anyone tell me what I am doing wrong here?


Solution

  • I realized what I was doing wrong. It was a stupid mistake. The JSON input string should have been in a key-value format. The correct input command is: peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n ${CC_NAME} --peerAddresses localhost:7051 --tlsRootCertFiles $PEER0_ORG1_CA -c '{"function": "createCar", "Args":"CAR101","{\"CarDet\":[{\"Make\": \"Honda\" , \"Model\": \"City\" , \"Colour\": \"White\"} , {\"Make\": \"Swift\" , \"Model\": \"Dezire\" , \"Colour\": \"Black\"}] , \"OwnerDet\": {\"Name\": \"James\" , \"Age\": \"29\" , \"Gender\": \"Male\"}}"]}