Switch node and JSONata (Solved)

I'm taking the wonderful course: A Developer's guide to Node-RED. And I've just learned about JSONata (very cool). I'm done with the homework assignment and this is not my homework. But I do have a question on using JSONata expressions in a switch node and using $globalContext('varNom').

I've found that if I set varNom to a value (let's say true) inside a node-red flow with global.set('varNom', true) that I can't use it in a JSONata expression
( Ex: $globalContext('varNom') = false).

Here's my example code

[{"id":"b4440ae7.c76a88","type":"change","z":"d275358f.727568","name":"(($globalContext('varNom')) = false)","rules":[{"t":"set","p":"payload","pt":"msg","to":"(($globalContext('varNom')) = false) ? \"An: 1\" : \"AN: 0\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":503.01422119140625,"y":405.00567626953125,"wires":[["56cf54ae.8737cc"]]}]

The flow is broken into 3 parts,

  • the first part is to set the varNom to true or false
  • the second is the test switch with
    1. JE: (($.payload.nom = "string") and ($globalContext('varNom')) = false)
    2. JE: ($.payload.nom = "string") and $globalContext('varNom')
    3. JE: ($.payload.nom = "string")
    4. Otherwise
    • I've selected Stop after first match .
  • in the third section you can see the equivalent result of the output using the change node

When I set varNom to false, the debug shows that the result is on output 2 (???). I'm expecting it to be on output 1.

When I set varNom to true, same output. I also tried setting the 2nd expression to $globalContext('varNom') = true but that returns false and output on 3. In my last section you can see the equivalent result of the output using the change node. I'm finding JSONata expressions to be very confusing.

So my question is, how do you use the JSONata expressions with $globalContext('varNom')?

Well, as often as not, posted a question and found the solution ... just after posting. Oh well.

I noticed in one of the debugs that the globalContext('varNom') returned an object. When I used glocalContext('varNom').payload I received it's value adn I can use that in the JSONata expression to evaluate true and false properly.

Note: I used a change node to set the global variable 'varNom' to true or false, so that may have something to do with the global variable being set to an object.

Huh, that sounds odd. Will investigate

@ncherry the expression $globalContext('varNom') ? "An: 1" : "AN: 0" works as you would want

Okay, more confusion because it does return true "AN 1" or false 'AN 0' correctly. Yet the expression $globalContext('varNom') = true is always false.

Okay, now my head hurts:

($globalContext('varNom').payload = false) ? "An: 1" : "AN: 0"

returns "AN: 0" for true and "AN: 1" for false ???

Opposite of ($globalContext('varNom') = false) ? "An: 1" : "AN: 0" which returns "AN: 0"

Here's what global.get('varNom') returns:

{
    "_msgid":"ff43ad6a.8653",
    "topic":"",
    "payload":{
        "_msgid":"b57df09d.d4c3e",
        "topic":"",
        "payload":true
    } 
}

So how exactly are you setting varNom?

I'm using the top change node, setting global.varNom to the jsonata expression $ .

That may explain the reason why varNom has an object (oops), but it doesn't explain (at least to me) the contradictory results.