# Mustache array problem

**URL:** <https://discourse.nodered.org/t/mustache-array-problem/74021>\
**Category:** General\
**Created:** [21 January 2023 21:06 UTC](https://discourse.nodered.org/t/mustache-array-problem/74021 "2023-01-21T21:06:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danieleronchi](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@danieleronchi](https://discourse.nodered.org/u/danieleronchi)\
**Post date:** [21 January 2023 21:06 UTC](https://discourse.nodered.org/t/mustache-array-problem/74021/1 "2023-01-21T21:06:27Z")

</div>

Good evening,  
i'm having some trouble about this: after a query in mysql i have this result `payload[2][0]["@id"]`, i need to get this data in a html template using mustache.  
How can i do it? `{{payload.array.2.0.@id}}` doesn't work.  
Any ideas?

Thank you!

---

<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:** [21 January 2023 21:12 UTC](https://discourse.nodered.org/t/mustache-array-problem/74021/2 "2023-01-21T21:12:47Z")

</div>

From the limited info  
`{{payload.2.0.@id}}`  
But can't be sure as i have no idea what your array really looks like.

---

<div class="post-metadata">

**Author:** ![danieleronchi](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@danieleronchi](https://discourse.nodered.org/u/danieleronchi)\
**Post date:** [21 January 2023 21:20 UTC](https://discourse.nodered.org/t/mustache-array-problem/74021/3 "2023-01-21T21:20:24Z")

</div>

Wow, it worked perfectly! Thank you so much!  
May i ask something else? Can i use this value in a if statement in html?  
For example: ( if {{payload.2.0.@id}} \> 0 ) { some html }

---

<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:** [21 January 2023 23:15 UTC](https://discourse.nodered.org/t/mustache-array-problem/74021/4 "2023-01-21T23:15:36Z")

</div>

There is a lambda function you could use. [mustache(5) - Logic-less templates.](http://mustache.github.io/mustache.5.html)  
But, it may just be simpler to use a [`string literal`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) template in a function node.

here is a example of three ways

```auto
[{"id":"d59e0171.98ced","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"[null,null,[{\"@id\":1}]]","payloadType":"json","x":130,"y":3920,"wires":[["f7e3199384a26e96","2a710bcb05a7a572","4af34fe8c4e91e62"]]},{"id":"f7e3199384a26e96","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload[2][0][\"@id\"]","pt":"msg","to":"$$.payload[2][0].`@id`> 0 ? \"<some html>\" : \"\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":3980,"wires":[["1f7af96e327ca29b"]]},{"id":"2a710bcb05a7a572","type":"function","z":"da8a6ef0b3c9a5c8","name":"function 20","func":"msg.payload = `<html>\n${(msg.payload[2][0][\"@id\"] > 0 ? \"<some html>\" : \"\")}\n</html>`\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":4080,"wires":[["00df87aa1a12b70e"]]},{"id":"4af34fe8c4e91e62","type":"function","z":"da8a6ef0b3c9a5c8","name":"function 21","func":"msg.wrapped = function() {\n return function(text, render) {\n text = Number(render(text)) > 0 ? \"<some html again>\" : \"\"\n return text\n }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":3920,"wires":[["71a414885e5a8651"]]},{"id":"96ebd467845b3c7c","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"[null,null,[{\"@id\":0}]]","payloadType":"json","x":130,"y":4020,"wires":[["f7e3199384a26e96","2a710bcb05a7a572","4af34fe8c4e91e62"]]},{"id":"1f7af96e327ca29b","type":"template","z":"da8a6ef0b3c9a5c8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n{{{payload.2.0.@id}}}\n</html>","output":"str","x":600,"y":3980,"wires":[["00df87aa1a12b70e"]]},{"id":"00df87aa1a12b70e","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 228","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":3980,"wires":[]},{"id":"71a414885e5a8651","type":"template","z":"da8a6ef0b3c9a5c8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n{{#wrapped}}\n{{payload.2.0.@id}}\n{{/wrapped}}\n\n</html>","output":"str","x":540,"y":3920,"wires":[["00df87aa1a12b70e"]]}]

```

---

<div class="post-metadata">

**Author:** ![danieleronchi](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@danieleronchi](https://discourse.nodered.org/u/danieleronchi)\
**Post date:** [22 January 2023 17:35 UTC](https://discourse.nodered.org/t/mustache-array-problem/74021/5 "2023-01-22T17:35:10Z")

</div>

Thank you so much E1cid, it's amazing!

---

<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:** [5 February 2023 17:35 UTC](https://discourse.nodered.org/t/mustache-array-problem/74021/6 "2023-02-05T17:35:42Z")

</div>

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