How to parse json received from Operation service result

Product/components used and version/fix level are you on:

Detailed explanation of the problem:

Hi Team,
I am trying to read result of c8y_command operation performed on my device, my device sends stringfy JSON data to cumulocity cloud as below.

‘{“status”:200,“data”:[{“time”:“2023-04-21T11:28:59.752429531Z”,“ackedTime”:“null”,“acknowledged”:false,“pointId”:“1”,“pointType”:“1”,“deviceId”:“140”,“deviceType”:“8”,“deviceName”:“BACnet/IP Device 140”,“mostRecentOccurence”:“2023/4/21 16:58:27:0”,“objectName”:“Object 1 1”,“priority”:2,“ackRequired”:0,“triggeredValue”:2,“recordId”:“a127792b-fb4b-4c72-bfff-bb392e8f076e”,“timestampType”:“2.0”,“weekday”:“5.0”,“eventType”:“Out Of Range”,“state”:“LowLimit”,“count”:2,“unit”:“No units”,“protocolType”:“BACnet”},{“time”:“2023-04-21T09:31:49.69681266Z”,“ackedTime”:“null”,“acknowledged”:false,“pointId”:“0”,“pointType”:“4”,“deviceId”:“77”,“deviceType”:“8”,“deviceName”:“BACnet/IP Device 77”,“mostRecentOccurence”:“2023/4/21 14:57:17:0”,“objectName”:“Object 4 0”,“priority”:2,“ackRequired”:1,“triggeredValue”:1,“recordId”:“f9a90664-ebd8-4208-856e-54e4f16b5c41”,“timestampType”:“2.0”,“weekday”:“5.0”,“eventType”:“Command Failure”,“state”:“Offnormal”,“count”:1,“unit”:“”,“protocolType”:“BACnet”},{“time”:“2023-04-21T09:31:10.330302972Z”,“ackedTime”:“null”,“acknowledged”:false,“pointId”:“0”,“pointType”:“1”,“deviceId”:“77”,“deviceType”:“8”,“deviceName”:“BACnet/IP Device 77”,“mostRecentOccurence”:“2023/4/21 14:56:26:0”,“objectName”:“Object 1 0”,“priority”:2,“ackRequired”:1,“triggeredValue”:120,“recordId”:“8c5ae43b-ffe8-4e7c-be5c-bd3173e27d1e”,“timestampType”:“2.0”,“weekday”:“5.0”,“eventType”:“Out Of Range”,“state”:“HighLimit”,“count”:2,“unit”:“No units”,“protocolType”:“BACnet”},{“time”:“2023-04-21T09:28:33.876507133Z”,“ackedTime”:“null”,“acknowledged”:false,“pointId”:“0”,“pointType”:“2”,“deviceId”:“77”,“deviceType”:“8”,“deviceName”:“BACnet/IP Device 77”,“mostRecentOccurence”:“2023/4/21 14:54:1:0”,“objectName”:“Object 2 0”,“priority”:2,“ackRequired”:0,“triggeredValue”:10,“recordId”:“b56d4019-25c8-44f3-8925-0ab499cfd0d8”,“timestampType”:“2.0”,“weekday”:“5.0”,“eventType”:“Out Of Range”,“state”:“LowLimit”,“count”:1,“unit”:“No units”,“protocolType”:“BACnet”}]}’

But same data when is try to read using operation service it becomes simple string.

“{status:200;data:[{time:2023-04-21T11:28:59.752429531Z;ackedTime:null;acknowledged:false;pointId:1;pointType:1;deviceId:140;deviceType:8;deviceName:BACnet/IP Device 140;mostRecentOccurence:2023/4/21 16:58:27:0;objectName:Object 1 1;priority:2;ackRequired:0;triggeredValue:2;recordId:a127792b-fb4b-4c72-bfff-bb392e8f076e;timestampType:2.0;weekday:5.0;eventType:Out Of Range;state:LowLimit;count:2;unit:No units;protocolType:BACnet};{time:2023-04-21T09:31:49.69681266Z;ackedTime:null;acknowledged:false;pointId:0;pointType:4;deviceId:77;deviceType:8;deviceName:BACnet/IP Device 77;mostRecentOccurence:2023/4/21 14:57:17:0;objectName:Object 4 0;priority:2;ackRequired:1;triggeredValue:1;recordId:f9a90664-ebd8-4208-856e-54e4f16b5c41;timestampType:2.0;weekday:5.0;eventType:Command Failure;state:Offnormal;count:1;unit:;protocolType:BACnet};{time:2023-04-21T09:31:10.330302972Z;ackedTime:null;acknowledged:false;pointId:0;pointType:1;deviceId:77;deviceType:8;deviceName:BACnet/IP Device 77;mostRecentOccurence:2023/4/21 14:56:26:0;objectName:Object 1 0;priority:2;ackRequired:1;triggeredValue:120;recordId:8c5ae43b-ffe8-4e7c-be5c-bd3173e27d1e;timestampType:2.0;weekday:5.0;eventType:Out Of Range;state:HighLimit;count:2;unit:No units;protocolType:BACnet};{time:2023-04-21T09:28:33.876507133Z;ackedTime:null;acknowledged:false;pointId:0;pointType:2;deviceId:77;deviceType:8;deviceName:BACnet/IP Device 77;mostRecentOccurence:2023/4/21 14:54:1:0;objectName:Object 2 0;priority:2;ackRequired:0;triggeredValue:10;recordId:b56d4019-25c8-44f3-8925-0ab499cfd0d8;timestampType:2.0;weekday:5.0;eventType:Out Of Range;state:LowLimit;count:1;unit:No units;protocolType:BACnet}]}”

Can any body help me to know how can i parse this operation service response as JSON.

Error messages / full error message screenshot / log fileL

Is your question related to the free trial, or to a production (customer) instance?

Have you installed all the latest fixes for the products and systems you are using?

Hello Deepak,

I cannot catch your question.

An operation has this life cycle:

  • Create the operation (Cumulocity) with POST {{url}}/devicecontrol/operations/
  • Send the operation to the device agent (Cumulocity)
    → Operation status is PENDING
  • Receive the operation (device agent)
  • Execute the operation (device agent)
  • Inform Cumulocity that the operation execution is started (device agent)
    with PUT {{url}}/devicecontrol/operations/{{operationId}} and body {“status”: “EXECUTING”}
    → Operation status is EXECUTING
  • Finish the operation (device agent)
  • Inform Cumulocity that the operation is finished successful or unsuccessful (device agent)
    with PUT {{url}}/devicecontrol/operations/{{operationId}} and body {“status”: “SUCCESSFUL” or “FAILED”}
    → Operation status is SUCCESSFUL or FAILED
  • every time you can request the operation status with GET {{url}}/devicecontrol/operations

To which of the steps belongs your question?

Kind regards,
Christian Herzog

@Christian_Herzog Exactly same life cycle is happening, i am creating C8y_Command, it’s going to device, device is setting status to “Executing first” then “Successful” or failed as per operation outcome.

Here my operation is Successful, with Successful Operation my device is sending response, which has been stringify at device level.

Cumulocity._cumulocityClient.publish(cumulocityConst.responseTopic + “/” + deviceId, cumulocityConst.templateCodes.setToSuccessful + “,” + cumulocityConst.c8yCommand + “,” +JSON.stringify(res).replaceAll(‘,’, ‘;’));

Stringify data at device looks like:
‘{“status”:200,“data”:[{“time”:“2023-04-21T11:28:59.752429531Z”,“ackedTime”:“null”,“acknowledged”:false,“pointId”:“1”,“pointType”:“1”,“deviceId”:“140”,“deviceType”:“8”,“deviceName”:“BACnet/IP Device 140”,“mostRecentOccurence”:“2023/4/21 16:58:27:0”,“objectName”:“Object 1 1”,“priority”:2,“ackRequired”:0,“triggeredValue”:2,“recordId”:“a127792b-fb4b-4c72-bfff-bb392e8f076e”,“timestampType”:“2.0”,“weekday”:“5.0”,“eventType”:“Out Of Range”,“state”:“LowLimit”,“count”:2,“unit”:“No units”,“protocolType”:“BACnet”},{“time”:“2023-04-21T09:31:49.69681266Z”,“ackedTime”:“null”,“acknowledged”:false,“pointId”:“0”,“pointType”:“4”,“deviceId”:“77”,“deviceType”:“8”,“deviceName”:“BACnet/IP Device 77”,“mostRecentOccurence”:“2023/4/21 14:57:17:0”,“objectName”:“Object 4 0”,“priority”:2,“ackRequired”:1,“triggeredValue”:1,“recordId”:“f9a90664-ebd8-4208-856e-54e4f16b5c41”,“timestampType”:“2.0”,“weekday”:“5.0”,“eventType”:“Command Failure”,“state”:“Offnormal”,“count”:1,“unit”:“”,“protocolType”:“BACnet”},{“time”:“2023-04-21T09:31:10.330302972Z”,“ackedTime”:“null”,“acknowledged”:false,“pointId”:“0”,“pointType”:“1”,“deviceId”:“77”,“deviceType”:“8”,“deviceName”:“BACnet/IP Device 77”,“mostRecentOccurence”:“2023/4/21 14:56:26:0”,“objectName”:“Object 1 0”,“priority”:2,“ackRequired”:1,“triggeredValue”:120,“recordId”:“8c5ae43b-ffe8-4e7c-be5c-bd3173e27d1e”,“timestampType”:“2.0”,“weekday”:“5.0”,“eventType”:“Out Of Range”,“state”:“HighLimit”,“count”:2,“unit”:“No units”,“protocolType”:“BACnet”},{“time”:“2023-04-21T09:28:33.876507133Z”,“ackedTime”:“null”,“acknowledged”:false,“pointId”:“0”,“pointType”:“2”,“deviceId”:“77”,“deviceType”:“8”,“deviceName”:“BACnet/IP Device 77”,“mostRecentOccurence”:“2023/4/21 14:54:1:0”,“objectName”:“Object 2 0”,“priority”:2,“ackRequired”:0,“triggeredValue”:10,“recordId”:“b56d4019-25c8-44f3-8925-0ab499cfd0d8”,“timestampType”:“2.0”,“weekday”:“5.0”,“eventType”:“Out Of Range”,“state”:“LowLimit”,“count”:1,“unit”:“No units”,“protocolType”:“BACnet”}]}’

now in my custom widget i am trying to get detail for same Operation and as a response i am getting simply string instead of JSON stringify string like this.

“{status:200;data:[{time:2023-04-21T11:28:59.752429531Z;ackedTime:null;acknowledged:false;pointId:1;pointType:1;deviceId:140;deviceType:8;deviceName:BACnet/IP Device 140;mostRecentOccurence:2023/4/21 16:58:27:0;objectName:Object 1 1;priority:2;ackRequired:0;triggeredValue:2;recordId:a127792b-fb4b-4c72-bfff-bb392e8f076e;timestampType:2.0;weekday:5.0;eventType:Out Of Range;state:LowLimit;count:2;unit:No units;protocolType:BACnet};{time:2023-04-21T09:31:49.69681266Z;ackedTime:null;acknowledged:false;pointId:0;pointType:4;deviceId:77;deviceType:8;deviceName:BACnet/IP Device 77;mostRecentOccurence:2023/4/21 14:57:17:0;objectName:Object 4 0;priority:2;ackRequired:1;triggeredValue:1;recordId:f9a90664-ebd8-4208-856e-54e4f16b5c41;timestampType:2.0;weekday:5.0;eventType:Command Failure;state:Offnormal;count:1;unit:;protocolType:BACnet};{time:2023-04-21T09:31:10.330302972Z;ackedTime:null;acknowledged:false;pointId:0;pointType:1;deviceId:77;deviceType:8;deviceName:BACnet/IP Device 77;mostRecentOccurence:2023/4/21 14:56:26:0;objectName:Object 1 0;priority:2;ackRequired:1;triggeredValue:120;recordId:8c5ae43b-ffe8-4e7c-be5c-bd3173e27d1e;timestampType:2.0;weekday:5.0;eventType:Out Of Range;state:HighLimit;count:2;unit:No units;protocolType:BACnet};{time:2023-04-21T09:28:33.876507133Z;ackedTime:null;acknowledged:false;pointId:0;pointType:2;deviceId:77;deviceType:8;deviceName:BACnet/IP Device 77;mostRecentOccurence:2023/4/21 14:54:1:0;objectName:Object 2 0;priority:2;ackRequired:0;triggeredValue:10;recordId:b56d4019-25c8-44f3-8925-0ab499cfd0d8;timestampType:2.0;weekday:5.0;eventType:Out Of Range;state:LowLimit;count:1;unit:No units;protocolType:BACnet}]}”

So problem is with parsing of response which i receive from Details metthod of Operation service.

Hi Deepak,

you should be able to do JSON.parse() to parse the JSON string back into an object in your widget. Though it would be better to not even stringify the response in the first place. instead create a custom field in the operation (e.g. “device_response”) and store the response as a JSON object there.

Best regards,
Harald

Could it be an issue of using MQTT and comma separated messages? As the json response also contains commas they might be misinterpreted. For that reason I guess this ugly thing JSON.stringify(res).replaceAll(‘,’, ‘;’)); has been done.
It might be better for that reason switching to JSON over MQTT or REST to update the operation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.