# Problem with global json object variable and JSONata switch decision

**URL:** https://discourse.nodered.org/t/problem-with-global-json-object-variable-and-jsonata-switch-decision/27361
**Category:** General
**Created:** [27 May 2020 08:07 UTC](https://discourse.nodered.org/t/problem-with-global-json-object-variable-and-jsonata-switch-decision/27361 "2020-05-27T08:07:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ljpaff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ljpaff/32/35020_2.png) [@ljpaff](https://discourse.nodered.org/u/ljpaff)
#### Post date: [27 May 2020 08:07 UTC](https://discourse.nodered.org/t/problem-with-global-json-object-variable-and-jsonata-switch-decision/27361/1 "2020-05-27T08:07:54Z")

</div>

Dear friends,

i have something strange. I am trying to make a switch decision with an Global JSON Object and JSONata condition. Problem is that in test zone resolves well but at execution time are not working as expected.

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

Global "presencia" variable is:

> {"abraham":"Casa","hellem":"Casa"}

Test Flow is:

```auto
[{"id":"c82ac7b3.f78e58","type":"tab","label":"TEST_ZONE","disabled":false,"info":""},{"id":"d92b3715.f42e58","type":"comment","z":"c82ac7b3.f78e58","name":"Control Deslumbrar Salón","info":"","x":330,"y":340,"wires":[]},{"id":"3dc69f34.17b28","type":"switch","z":"c82ac7b3.f78e58","name":"Presencia En Casa","property":"presencia","propertyType":"global","rules":[{"t":"jsonata_exp","v":"abraham = \"Casa\" or hellem = \"Casa\"","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":480,"wires":[["61908026.c932a"]]},{"id":"ebd16dfc.39067","type":"inject","z":"c82ac7b3.f78e58","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":480,"wires":[["3dc69f34.17b28"]]},{"id":"61908026.c932a","type":"debug","z":"c82ac7b3.f78e58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":740,"y":500,"wires":[\*]}]

```

What's wrong?

Best Regards

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [27 May 2020 09:15 UTC](https://discourse.nodered.org/t/problem-with-global-json-object-variable-and-jsonata-switch-decision/27361/2 "2020-05-27T09:15:28Z")

</div>

you need to use the **$globalContext** to get the global variable and if you use a single equal sigh, you will assign the value to the variable instead of testing it. The result could be true or false depending on what the result is.  
`$globalContext('abraham') == "Casa"`

---

<div class="post-metadata">

### Author: ![ljpaff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ljpaff/32/35020_2.png) [@ljpaff](https://discourse.nodered.org/u/ljpaff)
#### Post date: [27 May 2020 09:32 UTC](https://discourse.nodered.org/t/problem-with-global-json-object-variable-and-jsonata-switch-decision/27361/3 "2020-05-27T09:32:02Z")

</div>

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

JSONata doesn't like double equal for comparison into switch component...

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [27 May 2020 09:47 UTC](https://discourse.nodered.org/t/problem-with-global-json-object-variable-and-jsonata-switch-decision/27361/4 "2020-05-27T09:47:49Z")

</div>

Hi @ljpaff

the JSONata expression option in the Switch node is evaluated against the message it receives - regardless of what you set in the top 'property' box. This means you cannot point it at a context value and evaluate an expression against it. That is slightly surprising to me - I would have expected the JSONata expression to be evaluated against whatever you set in the top property box... I'll have a look to see if we can correct that - although would need to be careful not to change behaviour that people may rely on.

For your scenario, it doesn't matter what you put in the `property` box, but you can use the following expression:

```auto
$globalContext('presencia.abraham') = 'Casa' or $globalContext('presencia.hellem') = 'Casa'

```

- the single `=` is correct for a JSONata expression
- your value is stored under the context name of `presencia` - so you need to include that in the key passed to `$globalContext`

---

<div class="post-metadata">

### Author: ![ljpaff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ljpaff/32/35020_2.png) [@ljpaff](https://discourse.nodered.org/u/ljpaff)
#### Post date: [27 May 2020 09:55 UTC](https://discourse.nodered.org/t/problem-with-global-json-object-variable-and-jsonata-switch-decision/27361/5 "2020-05-27T09:55:21Z")

</div>

You are right, what is surprising to me is that if you use a payload from msg as property, inside a JSONata expression is populated.

This situation confused me because I was trying to use the same approach when I read a global variable

---

<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: [10 June 2020 09:55 UTC](https://discourse.nodered.org/t/problem-with-global-json-object-variable-and-jsonata-switch-decision/27361/6 "2020-06-10T09:55:21Z")

</div>

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