# Smart Rest Template for Operation

**URL:** <https://community.cumulocity.com/t/smart-rest-template-for-operation/755>\
**Category:** Forum\
**Tags:** thin-edge-io, device-connectivity, cumulocity\
**Created:** [February 14, 2024, 7:31am UTC](https://community.cumulocity.com/t/smart-rest-template-for-operation/755 "2024-02-14T07:31:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [February 14, 2024, 7:31am UTC](https://community.cumulocity.com/t/smart-rest-template-for-operation/755/1 "2024-02-14T07:31:40Z")

</div>

Hi,

I’m having this sample Operation in JSON:

```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:

```auto
<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?

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [February 14, 2024, 11:06am UTC](https://community.cumulocity.com/t/smart-rest-template-for-operation/755/2 "2024-02-14T11:06:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [February 14, 2024, 11:35am UTC](https://community.cumulocity.com/t/smart-rest-template-for-operation/755/3 "2024-02-14T11:35:32Z")

</div>

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

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/4/f/4f6cf724aa02361373925002566fab9a90dbadd5.png)

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).

---

<div class="post-metadata">

**Author:** ![Reuben\_Miller2](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/reuben_miller2/32/317_2.png) [@Reuben\_Miller2](https://community.cumulocity.com/u/Reuben_Miller2)\
**Post date:** [February 14, 2024, 12:36pm UTC](https://community.cumulocity.com/t/smart-rest-template-for-operation/755/4 "2024-02-14T12:36:32Z")

</div>

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:

```auto
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](http://thin-edge.io) [Cumulocity Proxy](https://thin-edge.github.io/thin-edge.io/references/cumulocity-proxy/).

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [February 14, 2024, 12:55pm UTC](https://community.cumulocity.com/t/smart-rest-template-for-operation/755/5 "2024-02-14T12:55:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Philipp\_Emmel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/philipp_emmel/32/19455_2.png) [@Philipp\_Emmel](https://community.cumulocity.com/u/Philipp_Emmel)\
**Post date:** [February 14, 2024, 1:45pm UTC](https://community.cumulocity.com/t/smart-rest-template-for-operation/755/6 "2024-02-14T13:45:20Z")

</div>

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](https://github.com/json-path/JsonPath)).

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.

---

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [February 16, 2024, 7:47am UTC](https://community.cumulocity.com/t/smart-rest-template-for-operation/755/7 "2024-02-16T07:47:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [August 14, 2024, 7:48am UTC](https://community.cumulocity.com/t/smart-rest-template-for-operation/755/8 "2024-08-14T07:48:12Z")

</div>

Closed on August 14, 2024, 7:48am UTC

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