Template vs Change Node: The for loop in JSONata

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?

[{"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"]]}]

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

try

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



Hi @ shrickus

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

[
  {
    "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]

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

[2]

<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 which @E1cid proposed and works really good.

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

Thanks both!

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