Tuesday, January 2, 2018

CHANGE node - Part 4 - Rule Change

The change rule from the change node is quite powerful and flexible.

It will always work on the value side of the key/value pair.

The rule will search for a pattern in the property value. If this pattern is matched then the value will be replaced by what you configure in the field "Replace with".

The "Replace with" value could be any hard-coded value (string, boolean, number, buffer) or any value from any other property from msg or flow or global objects.

You can imagine that the Change rule is like a powerful conditional statement that will check if the value of a property (configured in the field change)  matches the value from the field "Search for". If the match occurs then the value configured in the last field (Replace with) will be assigned to the original property.









Example #1: 
Change property  msg.payload.msg1.father to value "Frederic" if there is a character "f" inside the value of  msg.payload.msg1.father






Result:







Example #2: 
Change property  msg.payload.msg1.mother to value "Mary" if there is any character inside the value of  msg.payload.msg1.mother.

In this case, we can use a regular expression to search and check if there is a match.

Notice that the regular expression in the field "Search for" is just a dot. This elementary regular expression will result in a positive match if any character exists in the field "Search for".






Node-RED msg object before the rule:





Node-RED msg object after the rule:







Example #3: 
Change property  msg.payload.msg1.sister to equal whatever is the value of msg.payload.msg1.mother.


Taking advantage of the previous example I will add an additional change rule. I remind you that the order of the rules matter. The rules are verified in the sequence they appear in the configuration panel. By the way, you can easily change the order of the rules with a drag and drop operation. The drag and drop can be performed when you hover the mouse pointer on the icon with 3 lines that exist in front of the field "Search for".




Result: