# Creating comma separated string from JSON data with template node

**URL:** <https://discourse.nodered.org/t/creating-comma-separated-string-from-json-data-with-template-node/1073>\
**Category:** General\
**Created:** [25 June 2018 13:47 UTC](https://discourse.nodered.org/t/creating-comma-separated-string-from-json-data-with-template-node/1073 "2018-06-25T13:47:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fverschure](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fverschure/32/829_2.png) [@fverschure](https://discourse.nodered.org/u/fverschure)\
**Post date:** [25 June 2018 13:47 UTC](https://discourse.nodered.org/t/creating-comma-separated-string-from-json-data-with-template-node/1073/1 "2018-06-25T13:47:54Z")

</div>

I am trying to create a comma separated string with the template node from a JSON object.

I can get the comma's in, but these should not appear after the last item.  
I am using the code below in the template node:

> {{#payload}}{{name}} some text, {{/payload}}

is there any way to resolve this?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 June 2018 13:52 UTC](https://discourse.nodered.org/t/creating-comma-separated-string-from-json-data-with-template-node/1073/2 "2018-06-25T13:52:40Z")

</div>

You might find it easier to simply use the csv node?

---

<div class="post-metadata">

**Author:** ![fverschure](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fverschure/32/829_2.png) [@fverschure](https://discourse.nodered.org/u/fverschure)\
**Post date:** [25 June 2018 14:01 UTC](https://discourse.nodered.org/t/creating-comma-separated-string-from-json-data-with-template-node/1073/3 "2018-06-25T14:01:46Z")

</div>

I would like to use the CSV node if possible.  
My problem is that i use a JSON field than some text as one item which needs to be separated.  
Is that also possible with the CSV node?

---

<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:** [25 June 2018 14:54 UTC](https://discourse.nodered.org/t/creating-comma-separated-string-from-json-data-with-template-node/1073/4 "2018-06-25T14:54:34Z")

</div>

You are correct – the template node (well, mustache substitution, technically) is logicless, so it’s not a good choice unless you can append the same string for every element of the array.

Assuming your payload is an array of names, you can use this one-line javascript function:

```auto
msg.payload = msg.payload.join(" some text, ");
return msg;

```

Alternately, you could use a `change` node with this JSONata expression (using the `J:` pulldown)  
`$join(payload, " some text, ")`

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 June 2018 16:43 UTC](https://discourse.nodered.org/t/creating-comma-separated-string-from-json-data-with-template-node/1073/5 "2018-06-25T16:43:51Z")

</div>

> [@shrickus](#):
>
> one-line javascript function

What, no JSONata today Steve? 😉

---

<div class="post-metadata">

**Author:** ![fverschure](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fverschure/32/829_2.png) [@fverschure](https://discourse.nodered.org/u/fverschure)\
**Post date:** [25 June 2018 18:21 UTC](https://discourse.nodered.org/t/creating-comma-separated-string-from-json-data-with-template-node/1073/6 "2018-06-25T18:21:36Z")

</div>

I can actually add the same text around the data in the JSON array, that is not the issue.  
I need to export this as a string where each line is separated by a comma. The last element however should not have a comma. Still puzzeled how i could achieve this.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 June 2018 19:11 UTC](https://discourse.nodered.org/t/creating-comma-separated-string-from-json-data-with-template-node/1073/8 "2018-06-25T19:11:39Z")

</div>

> [@fverschure](#):
>
> The last element however should not have a comma. Still puzzeled how i could achieve this.

Using join won't leave you with a trailing comma unless you have an empty entry at the end of your data. Join only inserts **between** entries.
