# Route msg based on key name

**URL:** https://discourse.nodered.org/t/route-msg-based-on-key-name/15322
**Category:** General
**Created:** [9 September 2019 14:58 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322 "2019-09-09T14:58:02Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [9 September 2019 14:58 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/1 "2019-09-09T14:58:02Z")

</div>

Can I route a msg depending on its key name with basic nodes?

e.g

msg.payload is arriving on http in node as {"s3gpiocommand":"xxxx"} or {"s3gpioread":"yyyy"}

I'd like to process them differently in two separate branches

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [9 September 2019 15:20 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/2 "2019-09-09T15:20:39Z")

</div>

JSONata, I just did something similar. Switch node with JSONata set for both routes.  
The syntax you're looking for is

```auto
"s3gpiocommand" in $keys(payload)

```

and

```auto
"s3gpioread" in $keys(payload)

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [9 September 2019 15:23 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/3 "2019-09-09T15:23:16Z")

</div>

In 1.0 a new option has been added to the Switch node that lets you check for the presence of a given key name - which is just what you'd want.

An alternative JSONata approach to that suggested by @afelix is:

```auto
$exists($.payload.s3gpiocommand)

```

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [9 September 2019 15:25 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/4 "2019-09-09T15:25:40Z")

</div>

> [@afelix](#):
>
> Switch node with JSONata set for both routes.

Tried your solution but it didn't seem to work

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [9 September 2019 15:27 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/5 "2019-09-09T15:27:07Z")

</div>

Amazing, I couldn't find anything like an `$exists` in the JSONata docs so this is what I came up with:

```auto
[{"id":"2fc7d678.856452","type":"switch","z":"66c886df.ede628","name":"","property":"responseCookies","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"\"user_credentials\" in $keys($.responseCookies)","vt":"jsonata"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1410,"y":280,"wires":[["af6cd6cd.9c90b8","36d7fc85.f5c98c"],["55648ccd.d1538c"]]}]

```

~~can you see this is my first day using JSONata in pracice~~  
I had to do a POST request to a login endpoint on a server, and one of the ways I could see if the login succeeded was the presence of a cookie `"user_credentials"` in the response cookies. This is how I'm routing that exact situation.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [9 September 2019 15:27 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/6 "2019-09-09T15:27:54Z")

</div>

> [@knolleary](#):
>
> An alternative JSONata approach to that suggested by @afelix is:
> 
> ```auto
> $exists($.payload.s3gpiocommand)
> 
> ```

That doesn't seem to work either

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/abb16d42b53b20e9f24ac4d2f10a882fa97f7445.png)

I must be doing something daft!

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [9 September 2019 15:29 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/7 "2019-09-09T15:29:07Z")

</div>

> [@cymplecy](#):
>
> I must be doing something daft!

I have patent rights on that phrase 🙂

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [9 September 2019 15:30 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/8 "2019-09-09T15:30:02Z")

</div>

My test flow

```auto
[{"id":"2218a743.32e468","type":"inject","z":"aea6ecba.48c11","name":"","topic":"","payload":"{\"s3gpiocommand\":\"commandtest\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":660,"wires":[["73b0a755.159208"]]},{"id":"870eb8c1.d32ce8","type":"debug","z":"aea6ecba.48c11","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":640,"wires":[]},{"id":"73b0a755.159208","type":"switch","z":"aea6ecba.48c11","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"$exists($.payload.s3gpiocommand)","vt":"jsonata"},{"t":"eq","v":"$exists($.payload.s3gpioread)","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":2,"x":470,"y":680,"wires":[["870eb8c1.d32ce8"],["7bca6c62.cb4c94"]]},{"id":"7bca6c62.cb4c94","type":"debug","z":"aea6ecba.48c11","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":720,"wires":[]},{"id":"cf532957.da2818","type":"inject","z":"aea6ecba.48c11","name":"","topic":"","payload":"{\"s3gpioread\":\"readtest\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":720,"wires":[["73b0a755.159208"]]}]

```

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [9 September 2019 15:30 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/9 "2019-09-09T15:30:26Z")

</div>

> [@cymplecy](#):
>
> I must be doing something daft!

No, it's easier. Rather than testing with `==` set it to JSONata exp(ression)

What `==` with JSONata does is test if the contents of `msg.payload` are identical to the output of that expression (which will be true or false)

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [9 September 2019 15:32 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/10 "2019-09-09T15:32:47Z")

</div>

> [@cymplecy](#):
>
> $exists($.payload.s3gpiocommand)

Told you I was doing something daft 🙂 (copyright @ghayne )

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [9 September 2019 15:34 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/11 "2019-09-09T15:34:14Z")

</div>

Why not just use the 'has key' option in the switch  
 ![06%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7013e875bea3bbdd486e374cb59852e04de5e957.png)

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [9 September 2019 15:34 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/12 "2019-09-09T15:34:58Z")

</div>

@knolleary - in 1.0 the `JSONata exp` looks non-functional, when entering an expression, ok \> deploy, doubleclick the switch, the expression is gone.

@zenofmud that is only available in the beta.

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [9 September 2019 15:35 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/13 "2019-09-09T15:35:52Z")

</div>

It's not present in my 0.20.x version yet:

 ![28](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d170f41c7bf9f81997769683d1d32b71460faa7a.png)

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [9 September 2019 15:36 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/14 "2019-09-09T15:36:00Z")

</div>

> [@bakman2](#):
>
> in 1.0 the `JSONata exp` looks non-functional, when entering an expression

Yup, just noticed that myself. On the todo list.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [9 September 2019 15:36 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/15 "2019-09-09T15:36:26Z")

</div>

I refer you all to my original reply:

> [@knolleary](#):
>
> **In 1.0** a new option has been added to the Switch node that lets you check for the presence of a given key name - which is just what you'd want.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [9 September 2019 15:36 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/16 "2019-09-09T15:36:52Z")

</div>

> [@bakman2](#):
>
> @zenofmud that is only available in the beta.

just the more reason to start moving to 1.0 🤣

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [9 September 2019 15:38 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/17 "2019-09-09T15:38:35Z")

</div>

I ran into exactly this problem about half an hour ago, and as I had just started playing with JSONata (regex matching inside it actually) I tried to see if I could do this without a function node too. Just found the `$exists` in the docs, put as "boolean functions" while I tried to look for "object functions". Just realised the boolean functions is more about the return type than the type of content it works on, whereas the object functions are specifically about acting on objects

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [9 September 2019 15:39 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/18 "2019-09-09T15:39:06Z")

</div>

I'm playing with the Node-RED Desktop app which is currently using 0.20.7 so I need to use the JSONata approaches until 1.0 becomes official and it gets updated to it

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [9 September 2019 18:51 UTC](https://discourse.nodered.org/t/route-msg-based-on-key-name/15322/19 "2019-09-09T18:51:09Z")

</div>

Here are some other ways I know of to verify the existence of that field in JSONata:

```auto
    payload.s3gpioread != null

    $lookup(payload, "s3gpioread") ? true : false

    payload.s3gpioread.$length() > 0

    $boolean($keys(payload)~>$filter(/^s3gpioread$/)~>$count())

```

Not all are valid for all possible values of that key -- for instance, if a blank string is possible, and you just want to branch depending on the _presence_ of the field key, only the first and last expressions will work (besides the $exists version that Nick mentioned).
