JSONata - how to use msg.payload to lookup a value in a persistent flow object

I have a persistent flow object created in a function:

var osPath = [
        {"os" : "pi",      "path" : "/home/"},
        {"os" : "mac",     "path" : "/Users/"},
        {"os" : "windows", "path" : "c:/"},
        {"os" : "other",   "path" : "/home/"}
        ]
flow.set("osPath", osPath)

and based on the value in msg.payload I want to return the value of the 'path'. In a change node using JSONata, I can hard code the os value like: $flowContext("osPath")[os="windows"].path and it returns c:/ but I want to use msg.payload which I haven't been able to figure out. Any hints?

Since the change node exposes the full message object as $ in jsonata, try $.payload to access the payload. Typing from my phone so I can’t try it out, but see if $flowContext("osPath")[os=$.payload].path works.

If instead it were to put the flowContext as $, the message object aka the root object/input can still be accessed as $$, thus $$.payload would be my second guess.

1 Like

Ohhh, I was so hopeful but I still get an undefined

@afelix you almost had it right. I had to use $$ in the statement
$flowContext("osPath")[os=$.payload].path
Thanks for the nudge in the right direction

3 Likes

At the start of the expression, $$ has the input json object which in node-red means the full message object. It’s static as such, and you can always fall back to it. Once you move down the object tree, $ becomes the current object but as the flowContext is an extension to the language I wasn’t sure if it would take over the current object too.

1 Like