# How to access a message variable while using a JSONata expression in a change node?

**URL:** <https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589>\
**Category:** General\
**Created:** [18 February 2022 10:40 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589 "2022-02-18T10:40:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)\
**Post date:** [18 February 2022 10:40 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/1 "2022-02-18T10:40:12Z")

</div>

Hi all,

I'm trying to access the value of a message context while using a JSONata expression in a change node, but I can't happen to get it working.

Obviously, no problem with Flow or Global context since the JSON function exists, but how about simple message value ?

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

I would keep getting the following error:

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

To add on,

I have therefore tried to generate a flow variable instead, but the change node would reject the syntax :

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

And I don't want to have to use static decimal number such as 0, 1, 2, etc. as it makes my code way too static.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 February 2022 10:47 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/2 "2022-02-18T10:47:28Z")

</div>

Try something like below

```auto
$flowContext("blah.blah[" & $$.cueindex & "].balh")

```

---

<div class="post-metadata">

**Author:** ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)\
**Post date:** [18 February 2022 10:48 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/3 "2022-02-18T10:48:38Z")

</div>

Still not working ☹

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/5917783c0d5034c33d0117db1967f66b739a860d.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 February 2022 10:50 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/4 "2022-02-18T10:50:01Z")

</div>

I see no & or quotes closing the string

---

<div class="post-metadata">

**Author:** ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)\
**Post date:** [18 February 2022 10:51 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/5 "2022-02-18T10:51:13Z")

</div>

> [@E1cid](#):
>
> `" &`

Oh my god sorry I was too excited to try... haha, it seems to be working !!

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

Thank you so much, I'm gonna continue my testing !!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 February 2022 10:56 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/6 "2022-02-18T10:56:01Z")

</div>

I see you have mixed up quotes. If you use single then close with single

```auto
$flowContext('blah.blah[' & $$.cueindex & '].balh')

```

as you have shown is still incorrect and a single string no concatenation happens.

---

<div class="post-metadata">

**Author:** ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)\
**Post date:** [18 February 2022 10:58 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/7 "2022-02-18T10:58:57Z")

</div>

Alright, noted, thanks !

Now I'm having another issue in a template node this time.

I need to use that message variable inside an array, which is inside a flow message as follows.

But `msg.cueIndex` does not work:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/620ec5542c73c0c733a3754c284d8021eca1ba6d.png)

Neither `$$.cueIndex`, nor `{{cueIndex}}`, nor `{{msg.cueIndex}}`

Any idea ?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 February 2022 11:02 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/8 "2022-02-18T11:02:23Z")

</div>

First template node does not use [] for arrays.  
Second , i don't think that is possible in the template node. Best create a msg property from the flow var prior to the template node.

Or you could do your template in a change node with JSONata.

---

<div class="post-metadata">

**Author:** ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)\
**Post date:** [18 February 2022 11:08 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/9 "2022-02-18T11:08:32Z")

</div>

If I use single quotes everywhere, I'm getting the error again as follows:

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

Same, if I use double quotes everywhere, error is still here:

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 February 2022 11:14 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/10 "2022-02-18T11:14:03Z")

</div>

What happens if you  
set msg.payload to  
`'blah.blah[' & $$.cueIndex & '].blah'`  
what is the output, as it seems from the error it expects something before ], but throws error as it get ] and not what is expected.

Make sure no typo's of `cueIndex`

The blah being your properties not blah.

please post code as text as i can then copy it.

---

<div class="post-metadata">

**Author:** ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)\
**Post date:** [18 February 2022 11:31 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/11 "2022-02-18T11:31:16Z")

</div>

Thank you so much, got everything working correctly in the JSONata expression of the change node,

But I now need to find the simplest / cleanest way to use this message variable in the flow variable in the template node, best would be to be able to do everything inside.

But if I got no choice, will prepare the data outside in a change node, tho I don't really like the idea... lol

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 February 2022 13:14 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/12 "2022-02-18T13:14:17Z")

</div>

Not knowing the node configuration of your flow makes it difficult to give an definite ideas. in your change node can you not template there by concatenating strings and msg/context vars. Or as said define flow context to a msg var, then you can use the msg var in the template node.

---

<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:** [4 March 2022 13:15 UTC](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589/13 "2022-03-04T13:15:04Z")

</div>

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