Switch node Compare 2 keys

Hi,
Lets asume a simple setup of an inject button, a switch statement and a debug (for output).
The inject button injects a simple json:
{"key1": "valueA", "key2": "valueA"}

Now I want to validate in the switch node if key1 is equal key2:
condition should be something like msg.payload['key1'] == msg.payload['key2'].
I tried it using
property: msg.payload['key1']
equal msg.payload['key2']
But this does not work.

Is there any other option to perform this check in the switch-node?

Thanks for any help :slight_smile:

Hello,
Works for me no problem, I copy and pasted the json straight from your post into an inject set up in json mode and set the switch node like this:

[{"id":"8d0eda5.9c9f0a8","type":"switch","z":"4946ed1f.469644","name":"","property":"payload.key1","propertyType":"msg","rules":[{"t":"eq","v":"payload.key2","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":350,"y":120,"wires":[["7d3c28e0.562b38"]]},{"id":"4f851d42.25387c","type":"inject","z":"4946ed1f.469644","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"key1\": \"valueA\", \"key2\": \"valueA\"}","payloadType":"json","x":210,"y":120,"wires":[["8d0eda5.9c9f0a8"]]},{"id":"7d3c28e0.562b38","type":"debug","z":"4946ed1f.469644","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":120,"wires":[]}]

Johannes

Edit:
also works with the square bracket syntax:

Edit 2:
How did you setup your inject node? How us a debug of its output? Is it actually sending an object input as json or are you sending the actual json string?

Do you mean JSON (which means that it is a string) or is it a JavaScript Object? If it is JSON then feed it through a JSON node first, to convert it to an object.

1 Like

Thank you :slight_smile:
You are right, that works using the dotted syntax :slight_smile:
But unfortunately comparing 2 nested keys does not work:

[{"id":"c8272a46.29f618","type":"tab","label":"Flow 7","disabled":false,"info":""},{"id":"e9e3df88.d999a","type":"switch","z":"c8272a46.29f618","name":"","property":"payload.test2","propertyType":"msg","rules":[{"t":"eq","v":"12345","vt":"str"},{"t":"eq","v":"payload.test4","vt":"msg"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":280,"wires":[["bbc36929.65d278"],["d52d4950.e7bcf8"]]},{"id":"c6b06bcf.226d48","type":"inject","z":"c8272a46.29f618","name":"test","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"dd","payload":"{\"test\":\"2\",\"test3\":\"2\",\"test2\":{\"xx\":\"12345\"},\"test4\":{\"xx\":\"12345\"},\"payload\":\"...\"}","payloadType":"json","x":380,"y":160,"wires":[["e9e3df88.d999a"]]},{"id":"bbc36929.65d278","type":"debug","z":"c8272a46.29f618","name":"case1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":240,"wires":[]},{"id":"d52d4950.e7bcf8","type":"debug","z":"c8272a46.29f618","name":"case2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":340,"wires":[]}]

No because you can’t compare objects. You can only compare properties of objects.
So you would have to use the full path to the key you want to check.
In your example it would have to be setup like this:

Ok, Thank you :slight_smile:

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