# Build JSON out of variables

**URL:** <https://discourse.nodered.org/t/build-json-out-of-variables/58400>\
**Category:** General\
**Created:** [15 February 2022 15:14 UTC](https://discourse.nodered.org/t/build-json-out-of-variables/58400 "2022-02-15T15:14:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wuast94](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wuast94/32/23567_2.png) [@wuast94](https://discourse.nodered.org/u/wuast94)\
**Post date:** [15 February 2022 15:14 UTC](https://discourse.nodered.org/t/build-json-out-of-variables/58400/1 "2022-02-15T15:14:15Z")

</div>

I try to build Dynamic Notifications, my payload have to look like this:

```auto
{
   "extras": {
       "client::display": {
      "contentType": "text/markdown"
    },
       "client::notification": {
           "click": {"url": "$URL" }
        }
  },
   "message": "$MESSAGE.",
   "priority": 3,
   "title": "$TITLE"
}

```

as inputs i have for example a "msg.url" wich contains the url of a service

- $URL in my payload should be replaced by msg.url
- the $MESSAGE should be replaced with "msg.text" wich contains the text
- $TITLE should be replaced with the content from msg.title

Played arround with the JSON node, function node and googled, after an hour of try and error i hope that someone can bring me in the right direction 🙂

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [15 February 2022 15:24 UTC](https://discourse.nodered.org/t/build-json-out-of-variables/58400/2 "2022-02-15T15:24:33Z")

</div>

You can do this with the template node.  
eg

```auto
[{"id":"502b7419641df689","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"URL","v":"www.test.com","vt":"str"},{"p":"TITLE","v":"The Title","vt":"str"},{"p":"MESSAGE","v":"Test Message","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":140,"wires":[["8a4a3841.d3d04"]]},{"id":"8a4a3841.d3d04","type":"template","z":"bf9e1e33.030598","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n \"extras\": {\n \"client::display\": {\n \"contentType\": \"text/markdown\"\n },\n \"client::notification\": {\n \"click\": {\"url\": \"{{URL}}\" }\n }\n },\n \"message\": \"{{MESSAGE}}.\",\n \"priority\": 3,\n \"title\": \"{{TITLE}}\"\n}","output":"json","x":350,"y":140,"wires":[["8ad333f6.78443"]]},{"id":"8ad333f6.78443","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":140,"wires":[]}]

```

You may want to use triple {{{ if you don't want the text urlencoded.

Or you could use a change node with JSONata, or a function node with Javascript

---

<div class="post-metadata">

**Author:** ![wuast94](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wuast94/32/23567_2.png) [@wuast94](https://discourse.nodered.org/u/wuast94)\
**Post date:** [15 February 2022 15:34 UTC](https://discourse.nodered.org/t/build-json-out-of-variables/58400/3 "2022-02-15T15:34:59Z")

</div>

u are my hero today 🙂 thx 🙂

---

<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:** [15 February 2022 17:54 UTC](https://discourse.nodered.org/t/build-json-out-of-variables/58400/4 "2022-02-15T17:54:07Z")

</div>

You don't even need to use the template node. A JavaScript object can be turned into JSON using the JSON node 😀

That will be more robust than trying to do it yourself.

Of course, you can build a javascript object in many ways including change nodes (with or without JSONata) and function nodes.

---

<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:** [1 March 2022 17:54 UTC](https://discourse.nodered.org/t/build-json-out-of-variables/58400/5 "2022-03-01T17:54:31Z")

</div>

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