Append to payload message string

Somewhere I caught that "change" node has regular expression capabilities but not sure where that is documented, if it's true. At the moment I need to append and prepend to messages. I am currently doing this in JS in the function node, but that is a slower performance way (as I understand it) than using more efficient built in nodes like change.

Where can I find documentation on advanced string manipulation available - presumably in the change node? Meanwhile if you can provide answer to simple appending and prepending literal substring to msg.payload that would also be appreciated!

Hi @sarogersz

whilst its true to say the Function node does have a memory overhead, in practical terms, unless you're passing lots of messages through then it shouldn't be a problem.

But of course it's also good to learn what the core nodes can do for you!

There are a couple different ways to do this with the Change node - using a RegEx Change as you suggest, or a JSONata expression.

Regular Expression

Note the 'Search for' rule has been set to the regex type. The regular expression is set to match the entire value and capture it. The 'Replace with' field can then use $1 to represent the first capture group in the regex.

In terms of documentation for this, it depends how familiar you are with regular expressions. Once you know the trick with the capture groups being available as $1 $2 etc, you can do lots of different things (this bit is mentioned in the sidebar help for the change node)

JSONata Expression

This uses the JSONata expression language to join the strings using the & operator. For more about JSONata, you can read their documentation here.

5 Likes

For simple string substitution, there's always the template node -- it uses "mustache" syntax, so to prepend/append some static strings to a filename payload could be as simple as this one-liner:

/tmp/{{{payload}}}.png

N.B. triple braces cause the value to be substituted without any html escaping, which is the normal behavior if you use {{ ... }} doubles...

2 Likes

Thank you!

I am now using both function and change nodes. Will look at template nodes.