If/else in JSONata expressions

Its easy to assume you are more experienced (your posts seem more advanced than some of the other "beginners") - so a good sign :+1:

Plus, I find it better to offer answers that will help guide a user to the right place :slight_smile:

I cant understand first line and editor too:

sorry - typo (untested code)

remove the ; from the object definition...

msg.payload.data = {
   "brightness_pct" : msg.payload.brightness //JS object definition shouldnt have a ;  DOH!

Thats how you create an object in JS

var myObject = {}


let myOtherObject = {
  a_property: "a value for a_property"

that makes an object with a a_property already in it set to a string containing "a value for a_property"


msg.payload.data = {
   "brightness_pct" : msg.payload.brightness //JS object definition shouldnt have a ;  DOH!

basically means set msg.payload.data.brightness_pct = msg.payload.brightness; however, as data must be first created, we do it in one.

Another more explicit way would be ....

//first add a data property (object) to msg.payload
msg.payload.data = {}; //set data to a new empty object

//now add brightness_pct property to msg.payload.data
msg.payload.data.brightness_pct = msg.payload.brightness;

//next, if msg.payload.hue && payload.saturation are present, add hs_color property to msg.payload.data
if(msg.payload.hasOwnProperty("hue") && payload.hasOwnProperty("saturation")) {
   msg.payload.data.hs_color = [msg.payload.hue, msg.payload.saturation]; 

return msg;

For the jsonata version does hs_color need to be included or not ?
The jsonata snippet I wrote will not have hs_color at all if either saturation or hue is missing.

Sorry for my pause - I was temporary suspended :slight_smile:

I still had error and how I think if I not have hue or saturation json looks like this

   "brightness_pct" : payload.brightness,
   "hs_color" : 


And this error

Call-service API error.  Error Message: expected float for dictionary value @ data['hs_color']

@Steve-Mcl I try, got error and cant understand

"data": {
  "hs_color": [null,null]},
  "mydebughue":"not null","mydebugsaturation":"not null"

mydebug from this code:

if(new_val.hue !== null) {
        new_val.mydebughue = 'not null';

if(new_val.saturation !== null) {
        new_val.mydebugsaturation = 'not null';

I doing something wrong?

Some time later
Okay, I got it. I changed check from null to undefined and all looks okay

if(new_val.hue !== undefined && new_val.saturation !== undefined) {
        new_val.data.hs_color = [new_val.hue, new_val.saturation];


"mydebughue":"looks like null",
"mydebugsaturation":"looks like null"}

Thanks for help all, especially @Steve-Mcl :upside_down_face:

1 Like

@iStitch07 Sorry, to be a bit late on the party.

But the solution provided by @nlecaude in comment 3 seemed to do exactly what you initially asked
AND requires less code than function node.

Note that you can very easy validate/test your jsonata queries in change node in the "Test" tab (see example screenshot below).
So, if you want to test different possibilities - just edit the message in the "Example message" screen.

@janvda I just can't get away with jsonata. I can't fully explain or justify why I can't bring myself to invest any time in it at all. I have tinkered with it a couple of times but if anything, I much prefer alasql (run SQL like queries on js objects and arrays) to jsonata.

In my head its kinda like "I don't want to use another js bolt on when I can do it in standard js & it's much more readable" and "surely this parser is slower than straight js" and lastly I really dislike the syntax - it's just odd.

(The above are my opinion and irrational thoughts - nothing more)

I tried to use test tab, but result a little bit confused me

Your example message is not correct:

It should be:

{ "payload" :
      "brightness" : 0,
      "hue" :10,
      "saturation" : 10

Thank you. It’s really working. Not easy for dummies :slight_smile:
Because my current payload is huge and have all values, I will test and @nlecaude solution in parallel. Thank you for explanation :pray:


Note that if the input message is monitored by a "debug node" you can easily copy it and paste it in the "example message" tab of the jsonata expression editor window.

I’ve been using JSONata extensively for a while now and some of your points are totally valid.
On my side what I found is, if used well, it makes my flows a bit more consistent. In JavaScript there are a lot of ways to solve solutions where in JSONata it’s more limited. The fact it’s more limited often results in more similar structures and less chances of errors.

On the downsides, it is indeed slower and if speed is an issue JavaScript will always win.
Also if I end up with unusually long JSONata expressions they become harder to read, that’s where I’ll usually switch to JavaScript.

I also often use it a “smart” template in lieu of the builtin template object.

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