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

**URL:** <https://discourse.nodered.org/t/jsonata-how-to-use-msg-payload-to-lookup-a-value-in-a-persistent-flow-object/19738>\
**Category:** General\
**Created:** [30 December 2019 11:06 UTC](https://discourse.nodered.org/t/jsonata-how-to-use-msg-payload-to-lookup-a-value-in-a-persistent-flow-object/19738 "2019-12-30T11:06:02Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [30 December 2019 11:06 UTC](https://discourse.nodered.org/t/jsonata-how-to-use-msg-payload-to-lookup-a-value-in-a-persistent-flow-object/19738/1 "2019-12-30T11:06:02Z")

</div>

I have a persistent flow object created in a function:

```auto
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?

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [30 December 2019 11:10 UTC](https://discourse.nodered.org/t/jsonata-how-to-use-msg-payload-to-lookup-a-value-in-a-persistent-flow-object/19738/2 "2019-12-30T11:10:30Z")

</div>

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.

---

<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:** [30 December 2019 11:14 UTC](https://discourse.nodered.org/t/jsonata-how-to-use-msg-payload-to-lookup-a-value-in-a-persistent-flow-object/19738/3 "2019-12-30T11:14:50Z")

</div>

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

---

<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:** [30 December 2019 11:30 UTC](https://discourse.nodered.org/t/jsonata-how-to-use-msg-payload-to-lookup-a-value-in-a-persistent-flow-object/19738/4 "2019-12-30T11:30:13Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [30 December 2019 11:56 UTC](https://discourse.nodered.org/t/jsonata-how-to-use-msg-payload-to-lookup-a-value-in-a-persistent-flow-object/19738/5 "2019-12-30T11:56:32Z")

</div>

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.
