# JSONata - how to test if variable exists

**URL:** <https://discourse.nodered.org/t/jsonata-how-to-test-if-variable-exists/36900>\
**Category:** General\
**Created:** [2 December 2020 18:53 UTC](https://discourse.nodered.org/t/jsonata-how-to-test-if-variable-exists/36900 "2020-12-02T18:53:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [2 December 2020 18:53 UTC](https://discourse.nodered.org/t/jsonata-how-to-test-if-variable-exists/36900/1 "2020-12-02T18:53:24Z")

</div>

I have a simple JSONata expression in a Switch node like below. It works as expected when the flow context variable is defined and set to 'on'. But how can I, with JSONata, check if a flow or global context variable really exists and is defined. I have tried null, empty, undefined...

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

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [2 December 2020 19:17 UTC](https://discourse.nodered.org/t/jsonata-how-to-test-if-variable-exists/36900/2 "2020-12-02T19:17:03Z")

</div>

```auto
$exists($globalContext('test'))	

```

---

<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:** [2 December 2020 19:28 UTC](https://discourse.nodered.org/t/jsonata-how-to-test-if-variable-exists/36900/3 "2020-12-02T19:28:07Z")

</div>

Gary beat me to it... but I like a slightly different pattern:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/98adb54b08370a4c55107e0e4d4efb19875f4367.png)

Put the JSONata expression into the Property, and then use the output port rules as a kind of "case" statement (good for comparing incoming data to a list of possible string values, for example):

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

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [2 December 2020 19:32 UTC](https://discourse.nodered.org/t/jsonata-how-to-test-if-variable-exists/36900/4 "2020-12-02T19:32:33Z")

</div>

Brilliant Steve!

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [3 December 2020 05:52 UTC](https://discourse.nodered.org/t/jsonata-how-to-test-if-variable-exists/36900/5 "2020-12-03T05:52:35Z")

</div>

Wonderful!! Thank you both of you, really helpful, my Switch works as I want now!  
Best regards, Walter

---

<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:** [1 February 2021 05:52 UTC](https://discourse.nodered.org/t/jsonata-how-to-test-if-variable-exists/36900/6 "2021-02-01T05:52:40Z")

</div>

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