# How to access array element by index in mustache in template node

**URL:** <https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588>\
**Category:** General\
**Created:** [13 May 2020 10:49 UTC](https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588 "2020-05-13T10:49:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PlkMarudny](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plkmarudny/32/28315_2.png) [@PlkMarudny](https://discourse.nodered.org/u/PlkMarudny)\
**Post date:** [13 May 2020 10:49 UTC](https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588/1 "2020-05-13T10:49:21Z")

</div>

I have a simple flow that generates xml file using a template. But I have troubles using the data from the payload that holds the nested properties, as if only the first level of nesting is supported.  
The flow is below, the atrtibute "value" of the tag is empty in the resulting XML file. Am I missing something here?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3aef9a2aa779cdfa910c57cb4207f3b78e2877e3.png)

```auto
[{"id":"10864cf4.ba412b","type":"inject","z":"e4c94bd5.d53538","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":460,"wires":[["491ab592.6ffc24"]]},{"id":"491ab592.6ffc24","type":"function","z":"e4c94bd5.d53538","name":"build data","func":"msg.array = [{text: \"111 111 111 111\", template: { \n valueName : \"BreakingNews\",\n fields: [\"BreakingText\"]\n }\n },\n {text: \"222 222 222\", template: { \n valueName : \"BreakingNews\",\n fields: [\"BreakingText\"]\n }\n }\n];\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":460,"wires":[["fd6e7856.7c4f2","f6110285.4a7b18"]]},{"id":"fd6e7856.7c4f2","type":"template","z":"e4c94bd5.d53538","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<?xml version=\"1.0\"?>\n<tickerfeed version=\"2.4\">\n <playlist name=\"BreakingNews _______ \" target=\"carousel\" type=\"flipping_carousel\">\n <group>\n <description>Breaking News</description>\n <elements>\n {{#array}}\n <element>\n <template>{{{template.valueName}}}</template>\n <field name=\"{{{template.fields[0]}}}\">{{{text}}}</field>\n </element>\n {{/array}}\n </elements>\n </group>\n </playlist>\n</tickerfeed>","output":"str","x":460,"y":460,"wires":[["77413eda.bd5358"]]},{"id":"77413eda.bd5358","type":"debug","z":"e4c94bd5.d53538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":600,"y":460,"wires":[]},{"id":"f6110285.4a7b18","type":"debug","z":"e4c94bd5.d53538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":450,"y":400,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 May 2020 10:54 UTC](https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588/2 "2020-05-13T10:54:53Z")

</div>

Start in reverse.

Take the final expected XML through and XML node and see how it creates a JS object. Then do the reverse.

---

<div class="post-metadata">

**Author:** ![PlkMarudny](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plkmarudny/32/28315_2.png) [@PlkMarudny](https://discourse.nodered.org/u/PlkMarudny)\
**Post date:** [13 May 2020 11:18 UTC](https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588/3 "2020-05-13T11:18:37Z")

</div>

Well, the flow like below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/78f7492e5e86801ee2c40ab40d0f01ed4e6b6ce0.png)  
produces the right xml, but this is not the case here, let's skip the xml, simplified flow that shows the issue below, just the nested array value (template.fields[0]) is empty when used in template.

Data:

```auto
{
	"text": "sure",
	"template": {
		"fields": ["some name"]
	}
}

```

Template:

```auto
This works: {{{payload.text}}}, and this not: {{{payload.template.fields[0]}}}!

```

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

```auto
[{"id":"a6885cfc.a7045","type":"inject","z":"e4c94bd5.d53538","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":660,"wires":[["80cdd649.aa6a18"]]},{"id":"80cdd649.aa6a18","type":"change","z":"e4c94bd5.d53538","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{ \t\"text\": \"sure\", \t\"template\": { \t\t\"fields\": [\"some name\"] \t} }","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":660,"wires":[["3befd0f8.c7f39"]]},{"id":"3befd0f8.c7f39","type":"template","z":"e4c94bd5.d53538","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This works: {{{payload.text}}}, and this not: {{{payload.template.fields[0]}}}!","output":"str","x":480,"y":660,"wires":[["77d87954.9800a8"]]},{"id":"77d87954.9800a8","type":"debug","z":"e4c94bd5.d53538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":640,"y":660,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 May 2020 11:26 UTC](https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588/4 "2020-05-13T11:26:14Z")

</div>

use `.0` instead of `[0]`

```auto
[{"id":"12a4c5dc.63ca2a","type":"template","z":"dadb596f.70e948","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This works: {{{payload.text}}}, and this not: {{{payload.template.fields[0]}}} but this does... {{{payload.template.fields.0}}} !","output":"str","x":600,"y":100,"wires":[["cad92c2c.cb817"]]}]

```

```auto
This works: {{{payload.text}}}, and this too: {{{payload.template.fields.0}}}!

```

---

<div class="post-metadata">

**Author:** ![PlkMarudny](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plkmarudny/32/28315_2.png) [@PlkMarudny](https://discourse.nodered.org/u/PlkMarudny)\
**Post date:** [13 May 2020 11:28 UTC](https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588/5 "2020-05-13T11:28:24Z")

</div>

Indeed, it works, thank you very much!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 May 2020 11:30 UTC](https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588/6 "2020-05-13T11:30:24Z")

</div>

no worries.

I updated the title to assist future users (who face the same issue) find your post.

---

<div class="post-metadata">

**Author:** ![PlkMarudny](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/plkmarudny/32/28315_2.png) [@PlkMarudny](https://discourse.nodered.org/u/PlkMarudny)\
**Post date:** [13 May 2020 11:35 UTC](https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588/7 "2020-05-13T11:35:16Z")

</div>

Indeed, it now describes the issue correctly, thanks

---

<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:** [27 May 2020 11:35 UTC](https://discourse.nodered.org/t/how-to-access-array-element-by-index-in-mustache-in-template-node/26588/8 "2020-05-27T11:35:33Z")

</div>

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