# Switch node Compare 2 keys

**URL:** <https://discourse.nodered.org/t/switch-node-compare-2-keys/39166>\
**Category:** General\
**Created:** [15 January 2021 06:59 UTC](https://discourse.nodered.org/t/switch-node-compare-2-keys/39166 "2021-01-15T06:59:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![crackytsi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crackytsi/32/28369_2.png) [@crackytsi](https://discourse.nodered.org/u/crackytsi)\
**Post date:** [15 January 2021 06:59 UTC](https://discourse.nodered.org/t/switch-node-compare-2-keys/39166/1 "2021-01-15T06:59:51Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [15 January 2021 07:09 UTC](https://discourse.nodered.org/t/switch-node-compare-2-keys/39166/2 "2021-01-15T07:09:14Z")

</div>

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:

```auto
[{"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":[]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b01d5b22ebf0be628738407767b36384a7dc9b2.jpeg) ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/75379a6eba4acab53c506da2e38200ced31763bc.jpeg)

Johannes

Edit:  
also works with the square bracket syntax:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a76c73ec9425925c29a88da75caa6f3bed6c4ed.jpeg)

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?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f707125d9e94606afb36428f3c5ce3b73f92bdfc.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 January 2021 07:14 UTC](https://discourse.nodered.org/t/switch-node-compare-2-keys/39166/3 "2021-01-15T07:14:42Z")

</div>

> [@crackytsi](#):
>
> The inject button injects a simple json:

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.

---

<div class="post-metadata">

**Author:** ![crackytsi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crackytsi/32/28369_2.png) [@crackytsi](https://discourse.nodered.org/u/crackytsi)\
**Post date:** [15 January 2021 08:14 UTC](https://discourse.nodered.org/t/switch-node-compare-2-keys/39166/4 "2021-01-15T08:14:58Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [15 January 2021 08:22 UTC](https://discourse.nodered.org/t/switch-node-compare-2-keys/39166/5 "2021-01-15T08:22:51Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4feb3ad4f176551edb0faacfee57046640677425.jpeg)

---

<div class="post-metadata">

**Author:** ![crackytsi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crackytsi/32/28369_2.png) [@crackytsi](https://discourse.nodered.org/u/crackytsi)\
**Post date:** [15 January 2021 08:39 UTC](https://discourse.nodered.org/t/switch-node-compare-2-keys/39166/6 "2021-01-15T08:39:12Z")

</div>

Ok, Thank you 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 March 2021 08:39 UTC](https://discourse.nodered.org/t/switch-node-compare-2-keys/39166/7 "2021-03-16T08:39:22Z")

</div>

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