# IF/ELSE inside Expression

**URL:** <https://community.n8n.io/t/if-else-inside-expression/8090>\
**Category:** Questions\
**Created:** [September 26, 2021, 11:05pm UTC](https://community.n8n.io/t/if-else-inside-expression/8090 "2021-09-26T23:05:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cleveradmin](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/cleveradmin/32/4114_2.png) [@cleveradmin](https://community.n8n.io/u/cleveradmin)\
**Post date:** [September 26, 2021, 11:05pm UTC](https://community.n8n.io/t/if-else-inside-expression/8090/1 "2021-09-26T23:05:23Z")

</div>

Is it possible to do an if/else inside of an Expression? I have an expression that currently references {{$node[“Webhook”].json[“body”][“attributes”][“ticket\_id”]}}. The issue is that, as I’ve recently discovered, sometimes the data that comes in via the webhook uses ticket\_id and sometimes it uses just id. So what I’d like to have happen is that if {{$node[“Webhook”].json[“body”][“attributes”][“ticket\_id”]}} exists in the webhook data, it’s used, but if it does not exist it uses {{$node[“Webhook”].json[“body”][“attributes”][“id”]}} instead. In these situations where id is used instead of ticket\_id, ticket\_id is not there at all. Thank you.

---

<div class="post-metadata">

**Author:** ![shrey-42](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/shrey-42/32/3437_2.png) [@shrey-42](https://community.n8n.io/u/shrey-42)\
**Post date:** [September 27, 2021, 1:19am UTC](https://community.n8n.io/t/if-else-inside-expression/8090/2 "2021-09-27T01:19:18Z")

</div>

You could use this expression:

```auto
{{ $node["Webhook"].json["body"]["attributes"].hasOwnProperty("ticket_id")
? $node["Webhook"].json["body"]["attributes"]["ticket_id"] : $node["Webhook"].json["body"]["attributes"]["id"] }}

```

---

<div class="post-metadata">

**Author:** ![cleveradmin](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/cleveradmin/32/4114_2.png) [@cleveradmin](https://community.n8n.io/u/cleveradmin)\
**Post date:** [September 27, 2021, 4:48am UTC](https://community.n8n.io/t/if-else-inside-expression/8090/3 "2021-09-27T04:48:40Z")

</div>

This works, thanks Shrey, as always. FYI, some of your quotes are invalid so that would need to be cleaned up if anyone comes across this and it doesn’t work. Cheers!

---

<div class="post-metadata">

**Author:** ![shrey-42](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/shrey-42/32/3437_2.png) [@shrey-42](https://community.n8n.io/u/shrey-42)\
**Post date:** [September 27, 2021, 6:19am UTC](https://community.n8n.io/t/if-else-inside-expression/8090/4 "2021-09-27T06:19:55Z")

</div>

Thanks for letting me know that, Moez. Have now updated the quotes within the code.

---

<div class="post-metadata">

**Author:** ![sirdavidoff](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/sirdavidoff/32/2423_2.png) [@sirdavidoff](https://community.n8n.io/u/sirdavidoff)\
**Post date:** [March 17, 2023, 4:47pm UTC](https://community.n8n.io/t/if-else-inside-expression/8090/5 "2023-03-17T16:47:44Z")

</div>

You can now also use

```auto
{{ $if(condition, value_if_true, value_if_false) }}

```

Which is a little easier to read.

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/f/6/f66d81ebc6951de26bb242faaf6cae9aabc7fbed.png)

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [March 17, 2023, 5:13pm UTC](https://community.n8n.io/t/if-else-inside-expression/8090/6 "2023-03-17T17:13:53Z")

</div>


