# If/else in JSONata expressions

**URL:** https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194
**Category:** General
**Created:** [6 May 2020 22:16 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194 "2020-05-06T22:16:46Z")
**Posts on this page:** 13
**Page:** 2

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [7 May 2020 09:50 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/21 "2020-05-07T09:50:20Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![iStitch07](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/istitch07/32/21390_2.png) [@iStitch07](https://discourse.nodered.org/u/iStitch07)
#### Post date: [7 May 2020 10:04 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/22 "2020-05-07T10:04:44Z")

</div>

> [@Steve-Mcl](#):
>
> //first add a data property and set brightness\_pct to msg.payload.brightness msg.payload.data = { "brightness\_pct" : msg.payload.brightness; } //next, if msg.payload.hue && payload.saturation are present, add hs\_color property if(msg.payload.hasOwnProperty("hue") && payload.hasOwnProperty("saturation")) { msg.payload.data.hs\_color = [msg.payload.hue, msg.payload.saturation]; }

I cant understand first line and editor too:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/291604b3365ef1f0e270705e32fb62c2f59eab69.jpeg)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [7 May 2020 10:06 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/23 "2020-05-07T10:06:39Z")

</div>

sorry - typo (untested code)

remove the `;` from the object definition...

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

```

> [@iStitch07](#):
>
> I cant understand first line

Thats how you create an object in JS

`var myObject = {}`

or

```auto
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"

So,

```auto
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 ....

```auto
//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;

```

---

<div class="post-metadata">

### Author: ![nlecaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nlecaude/32/25_2.png) [@nlecaude](https://discourse.nodered.org/u/nlecaude)
#### Post date: [7 May 2020 11:23 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/24 "2020-05-07T11:23:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![iStitch07](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/istitch07/32/21390_2.png) [@iStitch07](https://discourse.nodered.org/u/iStitch07)
#### Post date: [8 May 2020 06:14 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/25 "2020-05-08T06:14:00Z")

</div>

Sorry for my pause - I was temporary suspended 🙂

 ![ADF29B73-29E9-414F-A354-DB12EAB1D6DA_1_201_a](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/269480506d89635897222531480c9781024255a1.jpeg)

> [@nlecaude](#):
>
> The jsonata snippet I wrote will not have hs\_color at all if either saturation or hue is missing.

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

```auto
{
   "brightness_pct" : payload.brightness,
   "hs_color" : 

}

```

And this error

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

```

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

```auto
{"brightness":100,
"data": {
  "brightness_pct":100,
  "hs_color": [null,null]},
  "mydebughue":"not null","mydebugsaturation":"not null"
}

```

mydebug from this code:

```auto
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

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

```

Debug

```auto
{"brightness":100,
"data":
   {"brightness_pct":100},
"mydebughue":"looks like null",
"mydebugsaturation":"looks like null"}

```

Thanks for help all, especially @Steve-Mcl 🙃

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [8 May 2020 07:56 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/26 "2020-05-08T07:56:38Z")

</div>

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

But the solution provided by @nlecaude in [comment 3](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/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.

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [8 May 2020 08:20 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/27 "2020-05-08T08:20:09Z")

</div>

@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)_

---

<div class="post-metadata">

### Author: ![iStitch07](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/istitch07/32/21390_2.png) [@iStitch07](https://discourse.nodered.org/u/iStitch07)
#### Post date: [8 May 2020 08:32 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/28 "2020-05-08T08:32:46Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3e3ab2cf665835756578937dc7933381c7a025ef.jpeg)

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [8 May 2020 08:38 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/29 "2020-05-08T08:38:12Z")

</div>

Your example message is not correct:

It should be:

```auto
{ "payload" :
    {
      "brightness" : 0,
      "hue" :10,
      "saturation" : 10
    }
}

```

---

<div class="post-metadata">

### Author: ![iStitch07](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/istitch07/32/21390_2.png) [@iStitch07](https://discourse.nodered.org/u/iStitch07)
#### Post date: [8 May 2020 08:44 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/30 "2020-05-08T08:44:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [8 May 2020 09:00 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/31 "2020-05-08T09:00:34Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/2/72a865dfd6c7e8567c6d5af66ab5ff79b610d925.png)

---

<div class="post-metadata">

### Author: ![nlecaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nlecaude/32/25_2.png) [@nlecaude](https://discourse.nodered.org/u/nlecaude)
#### Post date: [8 May 2020 13:04 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/32 "2020-05-08T13:04:40Z")

</div>

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.

---

<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: [22 May 2020 13:04 UTC](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194/33 "2020-05-22T13:04:44Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/if-else-in-jsonata-expressions/26194.md?page=1)
