Smart Rest Template for Operation

Hi,

I’m having this sample Operation in JSON:

{
  "deviceId": "2659458",
  "parameterWrite": {
    "parameters": [
      {
        "id": 1,
        "value": 21
      },
      {
        "id": 2,
        "value": 22
      },
      ....
    ]
  }
}

And want to create a Smart Rest Template for it in order to be received via MQTT (thin-edge).

I would want the MQTT message received by the device to look like:

<template-id>,<serial>,<param[1].id>,<param[1].value>,<param[2].id>,<param[2].value>,etc.
# so for above sample
<template-id>,<serial>,1,21,2,22

The tricky part for me is that the parameters array can be of any length. How would my Smart Rest Template definition look like here?

As far I know arrays/lists are not supported currently in response templates. So it’s not possible to implement your desired message structure unfortunately.

@Philipp_Emmel Might correct me here or can give you additional hints if I’m wrong.

I’m getting closer, currently I’m having this:

Which gives me the output: param1.id,param2.id,param1.value,param2.value. That’s something I could work with.

But I’d prefer the param1.id,param1.value,param2.id,param2.value format (a bit similar than how it’s done in 528 software-update static template).

1 Like

Defining key/value pairs from an array of objects does not seem to be supported in the custom SmartREST template builder.

However there is nothing wrong with using your proposal (e.g. id1,id2,val1,val2), apart from being “unexpected for a developer”, but that should not stop you from using it.

If you want to find out the array length by using the following formula:

parameter_count = (total_csv_fields - total_fixed_fields) / 2

Where the total_fixed_fields would be 2 in your case (as the first field is the message id 11, and the second field is the device’s external id).

Alternatively if you wanted to send a large list of parameters, then you could just send the URL as part of the SmartREST template where the operation handler could download the parameter list via the thin-edge.io Cumulocity Proxy.

1 Like

Interesting. I saw this [*] pattern somewhere, I think in the preview of the pattern field but I did not know that it is actually supported. Did you find this somewhere in the docs?

Technically these paths entered in Base pattern, Condition, Patterns, and other fields when configuring SmartREST templates are JSON paths (see: GitHub - json-path/JsonPath: Java JsonPath implementation).

We may not support the full scope of JSON Path and we also don’t use the most recent version. So there is always a bit of trial and error necessary to get things working exactly right especially with complex arrays like in this operation data model. The correct way to address what you what to address in JSON Path would be parameterWrite.parameters[*].[id,value]. However I was not able to make this work with SmartREST.

2 Likes

Thanks all, that’s useful information. Also did not succeed in making parameterWrite.parameters[*].[id,value] work in Smart Rest.
But that’s okay - I’m going to use the format that outputs id1,id2,value1,value2 then.