Using Array input to a Template node to generate a JSON formatted message

Hi Community,

i am stuck on this.

i have a payload in a form of an array of objects

i want to feed that into a template node to get a JSON formatted output
i am using the {{#paylaod}} & {{/payload}} inside the template node to iterate through all my array input objects.

all works well except for the last entry built by the template node. That's because the very last item will have a "comma" at the end of the object code and that mess up the JSON formatting

see the following example generated by my template node:

"rows":
[
{
"fields": [
"john",
"does",
"j.d@email.com"
]
},
{
"fields": [
"jane",
"doe",
"jane.doe@email.com"
]
},
]

that last "," after the jane.doe object which i tried to highlight in bold is the problem that is messing up my JSON output structure.

the above was generated with this code in my template node:

"rows":
[
{{#payload}}
{
"fields": [
"{{firstName}}",
"{{lastName}}",
"{{email}}"
]
},
{{/payload}}
]

So the question is, how do i get rid of that very last "," that is messing up the whole JSON structure???

image

hope this makes it bit more readable, it's that last "," highlighted in the picture that is causing me the trouble...

just a note in put case, my array is having almost a thousand objects... so simplicities sake, i am showing here only 2 objects,

Please supply a example flow with sample input data in an inject node and i will give you an example to omit last comma.

Thanks for reaching out.

attached is an example flow showing the problem.

JSON formatting in a Template Node.json (1.5 KB)

I appreciate your help :slight_smile:

The template node/mustache can only iterate over the elements, but cannot perform any logic/manipulation (or at least very limited)

Try a function node instead:

Input:

[{"firstName":"one","lastName":"one","email":"one@mail.com"},{"firstName":"two","lastName":"two","email":"two@mail.com"},{"firstName":"three","lastName":"three","email":"three@mail.com"},{"firstName":"four","lastName":"four","email":"four@mail.com"}]

Function node:

const input = msg.payload
const result = {}
result.content = {}
result.content.rows = input.map(x=>{ return { fields:[x.firstName,x.lastName,x.email] }})

msg.payload = result

return msg;

Output:

{ "content": { 
    "rows": 
    [
        { "fields": ["one", "one", "one@mail.com"] }, 
        { "fields": ["two", "two", "two@mail.com"] }, 
        { "fields": ["three", "three", "three@mail.com"] }, 
        { "fields": ["four", "four", "four@mail.com"] }] 
    }
}
2 Likes

First you have more than just a comma issue in your template. Missing quotes and object brackets. I have assumed content is an array due to these errors in template layout.

Second when posting flows it is best to use the </> icon and post between triple backticks as this adds a simple copy button , for ease of copying.

Here are two examples of how to do this.
Hope they help.

[{"id":"881abd96032c5eeb","type":"inject","z":"d1395164b4eec73e","name":"example payoad","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"firstName\":\"one\",\"lastName\":\"one\",\"email\":\"one@mail.com\"},{\"firstName\":\"two\",\"lastName\":\"two\",\"email\":\"two@mail.com\"},{\"firstName\":\"three\",\"lastName\":\"three\",\"email\":\"three@mail.com\"},{\"firstName\":\"four\",\"lastName\":\"four\",\"email\":\"four@mail.com\"}]","payloadType":"json","x":140,"y":720,"wires":[["cabf2ce109fd87c5","57b46d5e3607a2d2"]]},{"id":"cabf2ce109fd87c5","type":"template","z":"d1395164b4eec73e","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"content\": [\n            {\"rows\":\n                [\n                \"delete this\"\n                {{#payload}}\n                ,{\n                    \"fields\": [\n                        \"{{firstName}}\",\n                        \"{{lastName}}\",\n                        \"{{email}}\"\n                    ]\n                }\n                {{/payload}}   \n            ]\n        },\n        {\n            \"type\": \"toc\",\n            \"title\": \"Table of contents\",\n            \"levels\": 2\n        }\n    ]\n}","output":"json","x":320,"y":720,"wires":[["cefc7e45bef07918"]]},{"id":"57b46d5e3607a2d2","type":"function","z":"d1395164b4eec73e","name":"function 22","func":"const output = {\n    \"content\": [\n        {\n            \"rows\":\n            []\n        },\n        {\n            \"type\": \"toc\",\n            \"title\": \"Table of contents\",\n            \"levels\": 2\n        }\n    ]\n};\nmsg.payload.forEach(obj => {\n    output.content[0].rows.push(\n        {\n            \"fields\":[\n                obj.firstName,\n                obj.lastName,\n                obj.email\n            ]\n        }\n    )\n})\nmsg.payload = output;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":820,"wires":[["5635ef7dfd8c9052"]]},{"id":"cefc7e45bef07918","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"delete","p":"payload.content[0].rows[0]","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":720,"wires":[["0201468987664a63"]]},{"id":"5635ef7dfd8c9052","type":"debug","z":"d1395164b4eec73e","name":"debug 345","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":820,"wires":[]},{"id":"0201468987664a63","type":"debug","z":"d1395164b4eec73e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":720,"wires":[]}]
2 Likes

Thanks for the great help!!!!!

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