# How to build an Jsonata for this switch node?

**URL:** <https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836>\
**Category:** General\
**Created:** [13 September 2020 20:17 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836 "2020-09-13T20:17:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregor-samosir](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregor-samosir/32/27537_2.png) [@gregor-samosir](https://discourse.nodered.org/u/gregor-samosir)\
**Post date:** [13 September 2020 20:17 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/1 "2020-09-13T20:17:22Z")

</div>

Hi,  
i am new on NR and building up the first flows. Every thing is working, but some points can be made better, I think.

One question to start. I have an switch node to select the outputs based on the topics. For now I have 6 outputs, but would like to have the 3+4 and 5+6 on the same output. And its not enough to have an filter only on the part "cool\_" or "heat\_", this will match with to much topic.  
Is this a point to use Jsonata?

 ![Bildschirmfoto 2020-09-13 um 22.07.46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/2/725fad40cadebdec7b3b0c2a121e8405db1fc418.png)

---

<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:** [13 September 2020 20:52 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/2 "2020-09-13T20:52:29Z")

</div>

I think a regular expression test would do it, as you can match on cool\_ followed by target or diff. I am not going to tell you how to do it though, as I don't use regex much and would have to look it up and then play with it till it worked. So I will leave that for you to do yourself 🙂

Actually, if you order them correctly, which you already have, and select that it stops on the first match (the drop down at the bottom), then you just need to check for Contains heat\_ and Contains cool\_ as the other options will already have been ruled out. Unless there are other matching strings that you want to ignore completely that is.

---

<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:** [13 September 2020 21:03 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/3 "2020-09-13T21:03:17Z")

</div>

I connect switch outputs together when I want them both to be treated as being the same

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8ba9f94c5d411b98fece7cb2a79fc98ab883aede.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/187346e7b844555843d7edf2d11e286487242f8e.png)

note the stopping after first match setting

---

<div class="post-metadata">

**Author:** ![gregor-samosir](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregor-samosir/32/27537_2.png) [@gregor-samosir](https://discourse.nodered.org/u/gregor-samosir)\
**Post date:** [13 September 2020 21:19 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/4 "2020-09-13T21:19:55Z")

</div>

That's what I am using now. But I hope to find a solution for an thing like "if 'play' or 'pause' then use output" -\> 3

---

<div class="post-metadata">

**Author:** ![gregor-samosir](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregor-samosir/32/27537_2.png) [@gregor-samosir](https://discourse.nodered.org/u/gregor-samosir)\
**Post date:** [13 September 2020 21:21 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/5 "2020-09-13T21:21:41Z")

</div>

Hm, thanks. But to select that it stops on the first match is not an option for my flow.

---

<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:** [13 September 2020 21:28 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/6 "2020-09-13T21:28:29Z")

</div>

> [@gregor-samosir](#):
>
> stops on the first match is not an option for my flow.

Why not?

Each message can only have one value with one msg.topic, so why do you need to carry on looking for matches?

---

<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:** [13 September 2020 21:28 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/7 "2020-09-13T21:28:54Z")

</div>

> [@gregor-samosir](#):
>
> But to select that it stops on the first match is not an option for my flow.

I don't understand, how can it match more than one?

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [13 September 2020 21:32 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/8 "2020-09-13T21:32:10Z")

</div>

As @Colin said, regex is your "friend". Kind of... as it is not the friendliest stuff to use.  
However if you want to send 3 and 4 to the same output, you can do:

 ![Screenshot 2020-09-13 at 23.29.45](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd03e68280edbdb6c4c35b4354b0ee32b5d63a42.png)

If you want to experiment with regex, you can go to [https://regex101.com/](https://regex101.com/) it will help you to build the expression.

^ is beginning of string  
$ is end of string  
| is "or"

---

<div class="post-metadata">

**Author:** ![gregor-samosir](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregor-samosir/32/27537_2.png) [@gregor-samosir](https://discourse.nodered.org/u/gregor-samosir)\
**Post date:** [13 September 2020 21:42 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/9 "2020-09-13T21:42:50Z")

</div>

Thanks, this is the solution for me. Testet and it works. Have to spend some time on regex.

---

<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:** [14 September 2020 07:35 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/10 "2020-09-14T07:35:39Z")

</div>

You have not told us why the example selecting Stop at first match does not work? I suspect you may misunderstand what that does.

---

<div class="post-metadata">

**Author:** ![gregor-samosir](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregor-samosir/32/27537_2.png) [@gregor-samosir](https://discourse.nodered.org/u/gregor-samosir)\
**Post date:** [14 September 2020 09:01 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/11 "2020-09-14T09:01:54Z")

</div>

Right, my understanding for now is not deep enought. If you like, please give me some hints.

---

<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:** [14 September 2020 09:10 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/12 "2020-09-14T09:10:07Z")

</div>

Stop at first match means that it tests the first condition, if that is satisfied then it sends the message to output 1 and does nothing else. If the first test does not match then it tries the second test, and if that matches it sends the message to output 2 and does nothing else, and so on for each test. So in your original configuration if you selected this option then if it gets as far as test 5 then you would know that it was not cool\_switch\_state so need only test for Contains cool\_ to identify both cool\_target and cool\_diff.  
If you select Check all rules then it tests all the rules every time and will send the message to all outputs that match, not just the first one, which is obviously a waste of time in your original configuration as only one can match each time.

---

<div class="post-metadata">

**Author:** ![gregor-samosir](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregor-samosir/32/27537_2.png) [@gregor-samosir](https://discourse.nodered.org/u/gregor-samosir)\
**Post date:** [14 September 2020 09:28 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/13 "2020-09-14T09:28:39Z")

</div>

I understand. But There are more topics not shown in my first post also starting with cool\_ or heat\_ to implement later. So I have to take care on the order in this switch.  
But vor now its right to use the first match only.

---

<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:** [14 September 2020 10:07 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/14 "2020-09-14T10:07:16Z")

</div>

> [@gregor-samosir](#):
>
> There are more topics not shown in my first post also starting with cool\_ or heat\_

OK, in that case the regex method may be safer and easier to follow when you come back to look at it in a years time 🙂  
Though still it should be first match as that saves it wasting time when a match has been found.  
I think first match would be better as the default, but that is another matter.

---

<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:** [28 September 2020 10:07 UTC](https://discourse.nodered.org/t/how-to-build-an-jsonata-for-this-switch-node/32836/15 "2020-09-28T10:07:17Z")

</div>

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