Build JSON out of variables

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

{
   "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 :slight_smile:

You can do this with the template node.
eg

[{"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

1 Like

u are my hero today :slightly_smiling_face: thx :slightly_smiling_face:

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

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.

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