Switch node - jsonata - magic?

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

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!

7 Likes