# Switch node - jsonata - magic?

**URL:** <https://discourse.nodered.org/t/switch-node-jsonata-magic/12943>\
**Category:** General\
**Created:** [5 July 2019 07:35 UTC](https://discourse.nodered.org/t/switch-node-jsonata-magic/12943 "2019-07-05T07:35:00Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [5 July 2019 16:42 UTC](https://discourse.nodered.org/t/switch-node-jsonata-magic/12943/4 "2019-07-05T16:42:04Z")

</div>

I like to use a JSONata expression in a `switch` node's "Property", to essentially simulate a set of switch/case programming statements. For instance, to branch a flow based on how many objects are in the payload array (e.g. 0, 1, or multiple), I would use the expression `$count(payload)`, then have two tests against the numbers `0` and `1` on ports #1 and 2, then use the `Otherwise` test for port #3.

Here is another example that I've used in the past, to provide a "round-robin" routing node:

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

Every time a new msg enters this flow, it increments the flow counter, and routes it to a different path (e.g. different http server endpoints) based on the results of the "modulo" operator `%`. Typically, either the Property OR the tests would be an expression, not both -- but that's just because I've not had a case where I needed to check part of a payload against other properties of the msg object.

Hope this gives you a few new ideas!

---

_[View the full topic](https://discourse.nodered.org/t/switch-node-jsonata-magic/12943)._
