# Creating JSON with the template node

**URL:** <https://discourse.nodered.org/t/creating-json-with-the-template-node/30721>\
**Category:** General\
**Created:** [26 July 2020 10:34 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721 "2020-07-26T10:34:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![augjoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/augjoh/32/26464_2.png) [@augjoh](https://discourse.nodered.org/u/augjoh)\
**Post date:** [26 July 2020 10:34 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/1 "2020-07-26T10:34:07Z")

</div>

We are having some difficulties creating a JSON document with an `Template` node using mustache. There seems to be no way to create the `contact` array and producing valid JSON output:

```auto
   {
     "status": "valid",
     "contact": [
       "mailto:cert-admin@example.org",
       "mailto:admin@example.org"
     ],
     "termsOfServiceAgreed": true,
     "orders": "https://example.com/acme/orders/rzGoeA"
   }

```

Our current solution looks like this:

```auto
{
    "status": "{{ account.status }}",
    "contact": [
    {{#account.contacts}}
        "{{ email }}",
    {{/account.contacts}}
    ],
    "orders": "https://{{ domain }}/acme/{{ caname }}/orders"
}

```

But this creates an additional `,` at the end of the last array entry. It there any trick to using mustache for creating valid JSON arrays?

Is there any way to add another templating engine like handlebars?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 July 2020 10:48 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/2 "2020-07-26T10:48:58Z")

</div>

I don't think so. Mustache is made for text and although json strings are text, it does not have this kind of logic built in.

This is not hard to do with a function node instead.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 July 2020 11:47 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/3 "2020-07-26T11:47:26Z")

</div>

Where do you see the extra comma?

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [26 July 2020 11:55 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/4 "2020-07-26T11:55:10Z")

</div>

Would this solution work out for you? Probably not without modifying the object with jsonata first, so rendering it with jsonata might work out better instead 🤔

> <https://stackoverflow.com/questions/9089517/conditional-loop-in-mustache-js/9089728#9089728>

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [26 July 2020 11:56 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/5 "2020-07-26T11:56:56Z")

</div>

> [@augjoh](#):
>
> ```auto
> {{#account.contacts}}
> "{{ email }}",
> {{/account.contacts}}
> 
> ```

That wil loop through account.contacts and for every item in it, add the email property of each object in a string, ending with a comma. That comma in the last element of that loop is the issue as that wouldn’t be valid json

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 July 2020 11:58 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/6 "2020-07-26T11:58:20Z")

</div>

@augjoh you have a discrepancy in the data and the template. In the data you have `contact` while in the template you have the plural `contacts`

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [26 July 2020 12:00 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/7 "2020-07-26T12:00:42Z")

</div>

Not the case. Top is expected output. Bottom is the used template. No input data present 🙂

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 July 2020 12:02 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/8 "2020-07-26T12:02:31Z")

</div>

Ahhh right, then it would be nice to see what the actual data going into the template is

---

<div class="post-metadata">

**Author:** ![augjoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/augjoh/32/26464_2.png) [@augjoh](https://discourse.nodered.org/u/augjoh)\
**Post date:** [31 July 2020 07:14 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/9 "2020-07-31T07:14:26Z")

</div>

I've replaced the standard `template` node with `node-red-contrib-handlebars`. The following template handles the comma issue.

```auto
{
    "status": "{{ account.status }}",
    "orders": "https://{{ domain }}/acme/{{ caname }}/orders",
    "contact": [
        {{#each account.contacts }}
            "{{ . }}"{{#unless @last}},{{/unless}}
        {{/each}}
    ]
}

```

---

<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:** [14 August 2020 07:14 UTC](https://discourse.nodered.org/t/creating-json-with-the-template-node/30721/10 "2020-08-14T07:14:36Z")

</div>

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