Creating comma separated string from JSON data with template node

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?

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

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?

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:

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, ")

1 Like

What, no JSONata today Steve? :wink:

1 Like

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.

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.

1 Like