Insert a string into a change node string?

Insert a string into a change node string? I am addicted to function nodes, and trying to break the habit. So here is what has to be a bone-headed easy thing to do?

Say I have a message that has payload of 'test' and I have a change node where the set value is 'This is a X' where I want to replace the X with the incoming payload. I tried expression... 'This is a {{msg.payload}}' and I tried 'This is a ${msg.payload}' but did not get to work, what did I miss?

Why not using functions? They are there for these specific cases. In addition, I am convinced that a function Is faster than a change node.

Just trying to learn a new way... really function nodes are quick and easy... but when all I need is something simple like this... to me it is more "visually" communicative to use a change node. Function nodes make it very easy to hide a lot of details.

You could do it in a change node using JSONATA? See example below:

[{"id":"f592c255.9e6c1","type":"change","z":"2140a9d4.13a7fe","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"This is a \"& payload","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":932.3333435058594,"y":1392,"wires":[["5bbbfda8.0d77dc"]]},{"id":"40d6409e.c6e398","type":"inject","z":"2140a9d4.13a7fe","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test","payloadType":"str","x":726.3333435058594,"y":1392.0000381469727,"wires":[["f592c255.9e6c1"]]},{"id":"5bbbfda8.0d77dc","type":"debug","z":"2140a9d4.13a7fe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1147.3333435058594,"y":1390,"wires":[]}]

That is pretty much what I was asking. Did not realize it was just the raw message part name that worked. Over thinking it. How to do inset in the middle of the string? Just thought that would be useful as well? Say... "This ?? only a test." Where I want to replace the ??.

you can also use the Template node if you want to use the mustache syntax This is a {{payload}} !

Test flow

[{"id":"11a633e325e9c038","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test","payloadType":"str","x":230,"y":1540,"wires":[["e9623d404ebf31bf"]]},{"id":"9056a3d50c4598f7","type":"debug","z":"54efb553244c241f","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":1540,"wires":[]},{"id":"e9623d404ebf31bf","type":"template","z":"54efb553244c241f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This is a {{payload}} !","output":"str","x":420,"y":1540,"wires":[["9056a3d50c4598f7"]]}]

LOL... good suggestion... I just tried that literally as I think you were posting. So it can't be done via a change node? Just asking if there is some magic that would make it work?

yes .. the way @rakgupta demonstrated, using an expression ..
its just that jsonata uses & to concatenate the strings instead of + as in javascript.

1 Like

For sure you can use a change node, using JSONATA and $replace.

[{"id":"167e7587c990423b","type":"change","z":"53d8621916dd6725","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$replace('This is a X', 'X', payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1390,"y":1260,"wires":[["0c79ab821ad2769e"]]},{"id":"d58507f4a333f024","type":"inject","z":"53d8621916dd6725","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test","payloadType":"str","x":1210,"y":1260,"wires":[["167e7587c990423b"]]},{"id":"0c79ab821ad2769e","type":"debug","z":"53d8621916dd6725","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1590,"y":1260,"wires":[]}]

Instead of replacing X you can also replace any other pattern. :wink:

Yeah... like a message part? Straight Javascript in NR you could do "This is a ${msg.payload} test!" for example? Nuts... use replace... I should have thought of that.

What is funny... is I think the template methods is the most readable. Not that a function node is not valid.

If you dont want the replace function in JSONATA you can just combine with JSONATA:

I avoid to use function nodes as much as possible - as it contradicts the idea of a non coding programming tool.

At the end you could code each flow in one function node - but then you will loose all the fantastic possibilities to analyze your flow and see what is happening.

1 Like

Yup... that is pretty much where I am going... because I did significant programming at various points in the past... I tend to slip into that mind-set... and if I am going to publish a flow... I don't want 80% of it in one function node.... unless that is really what is needed.

Personally I don't share this vision. My target Is to realize a System Automation project as much fast and reliable as possibile. If I want to Watch a picture, I go to a Caravaggio's
exhibition! :smile:

1 Like

True, if I was doing 'production' different animal... but I see much of NR... as non-critical for my use cases and thus I can be flexible.

also it's good to try out the different ways of doing things - there are always different types of hammer in the toolbox.

1 Like

My rules...

#1 Know What You Don't Know, See 1000s of hours wasted, because IT people don't do solid analysis and evaluation of a problem, see vendors screaming at each other, all because someone is refusing to follow the facts.

#2 Sooner Or Later, All The Assholes Leave. They either stop being asses, or someone moves them out of the way for you. Many times, I had to tell someone, sometimes, not very nicely, that that their demands for action were slowing down the solution progress.

#3 If You Are Here, And No Likely Progress Or Solution, Go Back To Rule #1, You Missed Something.

#4 Divide And Conquer, Only change one thing at a time, break down the problem into simple easy to resolve issues, which is not always easy, but at 2am in the morning, and production has to be live at 6am, is not the time to start ignoring your rules.

After working in Fortune 10 firms for most of my IT career, these rules saved me countless hours of time, effort, and maybe most important, frustration. :slight_smile: Mentor and instructed many over the years using the above methodology, it has proven its worth, helping others, again and again.

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