# Template node javascript object evaluation - stringify

**URL:** <https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083>\
**Category:** General\
**Created:** [11 October 2021 10:11 UTC](https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083 "2021-10-11T10:11:43Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [11 October 2021 10:11 UTC](https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083/1 "2021-10-11T10:11:43Z")

</div>

I am trying to use a template node to setup a javascript object, eg.:

 ![Screenshot 2021-10-11 at 12.06.57](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/75951d994a10d5b015462c2f8a142dee0d1e4d60.jpeg)

I want to stringify it to make it available via a http endpoint.  
So I use a function node;

```auto
msg.payload = JSON.stringify(msg.payload)
return msg

```

output:

```auto
""{name: \"John Doe\"}""

```

This is not a valid JSON string.

If I use a function node and directly use an object:

```auto
let test = {name: "John Doe"}
msg.payload = JSON.stringify(test)
return msg;

```

output:

```auto
"{"name":"John Doe"}"

```

This is correct.

What is happening when the data leaves the template node that it does not evaluate to an object ?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 October 2021 10:15 UTC](https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083/2 "2021-10-11T10:15:35Z")

</div>

How have you configured the Template node? The default is to send the contents as plain text. If you want it to parse the template as JSON and to send the Object, you need to change the "Output as" option to "Parsed JSON".

But if you want it to do that, then you need to ensure the Template is valid JSON. Your current contents is not JSON - you need to put quotes around `name`:

```auto
{ "name": "John Doe"}

```

---

<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:** [11 October 2021 10:23 UTC](https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083/3 "2021-10-11T10:23:07Z")

</div>

What if I want to make the "object" from the template node available in a function node as an object ? (not necessarily as JSON)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 October 2021 10:29 UTC](https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083/4 "2021-10-11T10:29:34Z")

</div>

The Template node is for generating text.

As a convenience, if the text is valid JSON, then the node has an option to parse the generated text from JSON to a JavaScript object. If you didn't use that option, this is the same as putting a JSON node after the Template node.

So the only way to have the Template node send a JavaScript object is for you to enter JSON in the Template node and have it parse it for you.

---

<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:** [11 October 2021 10:35 UTC](https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083/5 "2021-10-11T10:35:49Z")

</div>

Ok thanks for making clear what I suspected.

---

<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:** [11 October 2021 11:17 UTC](https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083/6 "2021-10-11T11:17:21Z")

</div>

If you actually want it as a JSON string to make it available via a http endpoint, then your original config should do that, except that you need to put valid JSON in the template. So in the template put `{"name": "Fred"}` and _don't_ tell the template to convert it from JSON to an object. In the following function node you don't need the `JSON.stringify()` call as it is already JSON.

---

<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:** [11 October 2021 11:28 UTC](https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083/7 "2021-10-11T11:28:11Z")

</div>

Yes understood:

> What if I want to make the "object" from the template node available in a function node as an object ? (not necessarily as JSON)

I think i can solve this by using some regex.

Then again, setting up the template node with proper JSON is probably easier :')

---

<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:** [25 October 2021 11:28 UTC](https://discourse.nodered.org/t/template-node-javascript-object-evaluation-stringify/52083/8 "2021-10-25T11:28:37Z")

</div>

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