# Template vs Change Node: The for loop in JSONata

**URL:** https://discourse.nodered.org/t/template-vs-change-node-the-for-loop-in-jsonata/41093
**Category:** General
**Created:** [16 February 2021 14:55 UTC](https://discourse.nodered.org/t/template-vs-change-node-the-for-loop-in-jsonata/41093 "2021-02-16T14:55:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vongomben](https://avatars.discourse-cdn.com/v4/letter/v/a4c791/32.png) [@vongomben](https://discourse.nodered.org/u/vongomben)
#### Post date: [16 February 2021 14:55 UTC](https://discourse.nodered.org/t/template-vs-change-node-the-for-loop-in-jsonata/41093/1 "2021-02-16T14:55:23Z")

</div>

Hi!  
I'm making a request to an online but API.  
I'm trying to cicle trought the answer.

While I succeed using a template node, I am kinda lost on using a more elegant JSONata command.  
Any help?

 ![immagine](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/b/3bbf64383b4f9f9c1d296dda6a3b793581bafa0c.png)

```auto
[{"id":"2a2b15e6.2a632a","type":"inject","z":"63ccda68.bf7a74","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"740","payloadType":"str","x":370,"y":1320,"wires":[["671ffa20.6402b4"]]},{"id":"671ffa20.6402b4","type":"http request","z":"63ccda68.bf7a74","name":"BUS API","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://gpa.madbob.org/query.php?stop={{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":520,"y":1320,"wires":[["49d2fe0a.829be","ef439313.fc24a"]]},{"id":"49d2fe0a.829be","type":"template","z":"63ccda68.bf7a74","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{#payload}}\n<br> <strong> Line {{line}}</strong> is coming at <i> {{hour}}</i> \n{{/payload}}","output":"str","x":740,"y":1320,"wires":[["35747238.3d4e2e"]]},{"id":"35747238.3d4e2e","type":"debug","z":"63ccda68.bf7a74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1070,"y":1320,"wires":[]},{"id":"ef439313.fc24a","type":"change","z":"63ccda68.bf7a74","name":"Jsonata extraction (To Be Arrayed)","rules":[{"t":"set","p":"payload","pt":"msg","to":"' Line ' & payload[0].line & \t' in coming at ' & payload[0].hour &\t\"<br>\" &\t' Line ' & payload[1].line & \t' in coming at ' & payload[1].hour &\t\"<br>\" &\t' Line ' & payload[2].line & \t' in coming at ' & payload[2].hour &\t\"<br>\" &\t' Line ' & payload[3].line & \t' in coming at ' & payload[3].hour &\t\"<br>\" &\t' Line ' & payload[4].line & \t' in coming at ' & payload[4].hour \t\t\t\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":1360,"wires":[["35747238.3d4e2e"]]}]

```

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [16 February 2021 15:18 UTC](https://discourse.nodered.org/t/template-vs-change-node-the-for-loop-in-jsonata/41093/2 "2021-02-16T15:18:48Z")

</div>

Can you post some sample input data, and some information about what the output should be?

Most of us are willing to help, especially if we don't have to install an entire flow to call some unknown business api...

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [16 February 2021 15:35 UTC](https://discourse.nodered.org/t/template-vs-change-node-the-for-loop-in-jsonata/41093/3 "2021-02-16T15:35:00Z")

</div>

try

```auto
$join(
   payload.$.(
       '<strong>Line ' & $.line & 
'</strong> in coming at <i>' & $.hour &
"</li><br>"
   )
)

```

---

<div class="post-metadata">

### Author: ![vongomben](https://avatars.discourse-cdn.com/v4/letter/v/a4c791/32.png) [@vongomben](https://discourse.nodered.org/u/vongomben)
#### Post date: [16 February 2021 16:07 UTC](https://discourse.nodered.org/t/template-vs-change-node-the-for-loop-in-jsonata/41093/4 "2021-02-16T16:07:28Z")

</div>

Hi @ [shrickus](https://discourse.nodered.org/u/shrickus)

The data I receive is this from the API call is:

```auto
[
  {
    "line": "17",
    "hour": "17:13",
    "realtime": "true"
  },
  {
    "line": "17",
    "hour": "17:39",
    "realtime": "false"
  },
  {
    "line": "17",
    "hour": "18:06",
    "realtime": "false"
  },
  {
    "line": "17B",
    "hour": "17:26",
    "realtime": "true"
  },
  {
    "line": "17B",
    "hour": "17:52",
    "realtime": "false"
  },
  {
    "line": "17B",
    "hour": "18:19",
    "realtime": "false"
  },
  {
    "line": "66",
    "hour": "17:00",
    "realtime": "true"
  },
  {
    "line": "66",
    "hour": "17:26",
    "realtime": "false"
  },
  {
    "line": "66",
    "hour": "17:53",
    "realtime": "false"
  }
]

```

I'm succeding in trasforming into this[2], using this template in mustache[1]  
[1]

```auto
{{#payload}}
<br> <strong> Line {{line}}</strong> is coming at <i> {{hour}}</i> 
{{/payload}}

```

[2]

```auto
<br> <strong> Line 17</strong> is coming at <i> 17:13</i> 
<br> <strong> Line 17</strong> is coming at <i> 17:39</i> 
<br> <strong> Line 17</strong> is coming at <i> 18:06</i> 
<br> <strong> Line 17B</strong> is coming at <i> 17:26</i> 
<br> <strong> Line 17B</strong> is coming at <i> 17:52</i> 
<br> <strong> Line 17B</strong> is coming at <i> 18:19</i> 
<br> <strong> Line 66</strong> is coming at <i> 17:00</i> 
<br> <strong> Line 66</strong> is coming at <i> 17:26</i> 
<br> <strong> Line 66</strong> is coming at <i> 17:53</i> 

```

What I would like to achieve with a JSONata expression using [join](https://docs.jsonata.org/string-functions.html#join) which @E1cid proposed and works really good.

```auto
$join(
   payload.$.(
       '<strong>Line ' & $.line & 
'</strong> in coming at <i>' & $.hour &
"</li><br>"
   )
)

```

Thanks both!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [17 April 2021 16:07 UTC](https://discourse.nodered.org/t/template-vs-change-node-the-for-loop-in-jsonata/41093/5 "2021-04-17T16:07:36Z")

</div>

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