# Help with inserting payload in template

**URL:** https://discourse.nodered.org/t/help-with-inserting-payload-in-template/40443
**Category:** General
**Created:** [5 February 2021 14:19 UTC](https://discourse.nodered.org/t/help-with-inserting-payload-in-template/40443 "2021-02-05T14:19:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)
#### Post date: [5 February 2021 14:19 UTC](https://discourse.nodered.org/t/help-with-inserting-payload-in-template/40443/1 "2021-02-05T14:19:39Z")

</div>

Hi. I want to insert payload from an xml in a template node.  
I have succseded with a ui form but i don´t know why this is not working?

 ![payload5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc7190db3ecf6d05d0656bcd65396225da841116.jpeg)

```auto
[{"id":"f53853fa.2a74","type":"template","z":"75a648ae.abdc08","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>","output":"str","x":1200,"y":700,"wires":[["57d01be.b3113e4"]]},{"id":"57d01be.b3113e4","type":"xml","z":"75a648ae.abdc08","name":"","property":"payload","attr":"","chr":"","x":1330,"y":700,"wires":[["5ce3f42f.58da1c"]]},{"id":"aace4f7e.4f8b9","type":"inject","z":"75a648ae.abdc08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1030,"y":700,"wires":[["f53853fa.2a74"]]},{"id":"51b231a1.24083","type":"template","z":"75a648ae.abdc08","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"My Name is {{{payload[0]}}} {{{payload[1]}}}","output":"str","x":1800,"y":700,"wires":[["9f57c458.80fc88"]]},{"id":"42560560.773f7c","type":"join","z":"75a648ae.abdc08","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1650,"y":700,"wires":[["51b231a1.24083"]]},{"id":"9f57c458.80fc88","type":"debug","z":"75a648ae.abdc08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1950,"y":700,"wires":[]},{"id":"5ce3f42f.58da1c","type":"function","z":"75a648ae.abdc08","name":"Testing","func":"var msg1 = { payload:msg.payload.note.to[0] };\nvar msg2 = { payload:msg.payload.note.from[0] };\nvar msg3 = { payload:msg.payload.note.heading[0] };\nvar msg4 = { payload:msg.payload.note.body[0] };\nreturn [msg1, msg2, msg3, msg4];\n","outputs":4,"noerr":0,"initialize":"","finalize":"","x":1500,"y":700,"wires":[["42560560.773f7c"],["42560560.773f7c"],["42560560.773f7c"],["42560560.773f7c"]]}]

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 February 2021 14:32 UTC](https://discourse.nodered.org/t/help-with-inserting-payload-in-template/40443/2 "2021-02-05T14:32:58Z")

</div>

Show us what is going into the template and what comes out. That is more important than the flow, initially.

---

<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: [5 February 2021 14:38 UTC](https://discourse.nodered.org/t/help-with-inserting-payload-in-template/40443/3 "2021-02-05T14:38:55Z")

</div>

The reason it's not working is template does not accept `[0]`, it would be `payload.0.0` etc

here is an example one simple way and your way with function and join node, hope it helps

```auto
[{"id":"5540a6e2.39cfc","type":"inject","z":"57675e72.26a0d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":4880,"wires":[["59eaf72a.f966a8"]]},{"id":"59eaf72a.f966a8","type":"template","z":"57675e72.26a0d8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>","output":"str","x":310,"y":4880,"wires":[["46981544.d0d3ec"]]},{"id":"46981544.d0d3ec","type":"xml","z":"57675e72.26a0d8","name":"","property":"payload","attr":"","chr":"","x":440,"y":4880,"wires":[["b32682e1.41b188","bd8302b8.a4ca58"]]},{"id":"b32682e1.41b188","type":"template","z":"57675e72.26a0d8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"My Name is {{{ payload.note.to }}} {{{payload.note.from}}}","output":"str","x":590,"y":4880,"wires":[["8d869a47.30354"]]},{"id":"bd8302b8.a4ca58","type":"function","z":"57675e72.26a0d8","name":"Testing","func":"var msg1 = { payload:msg.payload.note.to };\nvar msg2 = { payload:msg.payload.note.from };\nvar msg3 = { payload:msg.payload.note.heading };\nvar msg4 = { payload:msg.payload.note.body };\nreturn [msg1, msg2, msg3, msg4];\n","outputs":4,"noerr":0,"initialize":"","finalize":"","x":600,"y":4800,"wires":[["d0732c01.2e611"],["d0732c01.2e611"],["d0732c01.2e611"],["d0732c01.2e611"]]},{"id":"8d869a47.30354","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":4880,"wires":[]},{"id":"d0732c01.2e611","type":"join","z":"57675e72.26a0d8","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":750,"y":4820,"wires":[["74eae1b.5892e2"]]},{"id":"74eae1b.5892e2","type":"template","z":"57675e72.26a0d8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"My Name is {{{ payload.0.0 }}} {{{payload.1.0}}}","output":"str","x":890,"y":4820,"wires":[["8d869a47.30354"]]}]

```

Note the speed difference.

[edit]  
there is a slight error in my code, in the template is should be `payload.note.to.0` not `payload.note.to` and the same with  
`payload.note.tfrom` it works the without the 0 as it displays the array with single item.

---

<div class="post-metadata">

### Author: ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)
#### Post date: [8 February 2021 13:21 UTC](https://discourse.nodered.org/t/help-with-inserting-payload-in-template/40443/4 "2021-02-08T13:21:31Z")

</div>

Thanks!! Works great!

/Regards Niclas

---

<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: [9 April 2021 13:22 UTC](https://discourse.nodered.org/t/help-with-inserting-payload-in-template/40443/5 "2021-04-09T13:22:10Z")

</div>

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