# How to use/access a message variable to navigate an array with a JSONata expression in a switch node?

**URL:** <https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907>\
**Category:** General\
**Created:** [21 May 2022 09:13 UTC](https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907 "2022-05-21T09:13:40Z")\
**Posts on this page:** 8\
**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:** [21 May 2022 09:13 UTC](https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907/1 "2022-05-21T09:13:41Z")

</div>

Hello all,

I'm struggling trying to use/access a message variable to navigate an array with a JSONata expression in a switch node.

The message variable is "index", and is well declared.

I've tried all I could, but it is not working:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/2/02bfafe063e0f51f98138278e76533bcf50efcee.png)

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

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

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

I've had a similar issue in the past, fixed [here](https://discourse.nodered.org/t/how-to-access-a-message-variable-while-using-a-jsonata-expression-in-a-change-node/58589) but the problem/requirement was slightly different since I was using a change node, and flowContext.

code is attached  
[flows.json](https://discourse.nodered.org/uploads/short-url/cuyHAsFTndiZ6z1bPVl6RPUZQg0.json) (7.0 KB)

Thanks

---

<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:** [21 May 2022 10:38 UTC](https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907/2 "2022-05-21T10:38:40Z")

</div>

You have not supplied any incoming data and no one can copy the text in those images.  
image 3 looks closest  
`$$.payload.streams[$$.index].tags.langauge` should work at a guess as i have no idea of your data. I don't think you can mix $$ and msg, one or the other, msg will put jsonata in compatibility mode.

---

<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:** [21 May 2022 10:46 UTC](https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907/3 "2022-05-21T10:46:13Z")

</div>

Sorry @E1cid

here is the code 🙂

[flows.json](https://discourse.nodered.org/uploads/short-url/cuyHAsFTndiZ6z1bPVl6RPUZQg0.json) (7.0 KB)

---

<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:** [21 May 2022 10:59 UTC](https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907/4 "2022-05-21T10:59:46Z")

</div>

Looking at your flow is very confusing, What exactly are you trying to do?

---

<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:** [21 May 2022 11:12 UTC](https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907/5 "2022-05-21T11:12:08Z")

</div>

Hi @E1cid

Im trying to move the parameter streams[index].tag.language to streams[index].language, if it exists.

---

<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:** [21 May 2022 11:37 UTC](https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907/6 "2022-05-21T11:37:36Z")

</div>

I would use the transform function

```auto
[{"id":"a9348d92797fed33","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":510,"y":1940,"wires":[["e353ab05c11e2d03"]]},{"id":"e353ab05c11e2d03","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"program_id\":16,\"program_num\":16,\"pcr_pid\":620,\"streams\":[{\"codec_type\":\"video\",\"id\":\"0x26c\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0}},{\"codec_type\":\"audio\",\"id\":\"0x277\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"eng\"}},{\"codec_type\":\"audio\",\"id\":\"0x278\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"qab\"}},{\"codec_type\":\"subtitle\",\"id\":\"0x280\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"eng\"}},{\"codec_type\":\"subtitle\",\"id\":\"0x281\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"zho\"}},{\"codec_type\":\"subtitle\",\"id\":\"0x282\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"msa\"}},{\"codec_type\":\"subtitle\",\"id\":\"0x283\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"tam\"}},{\"id\":\"0x2a8\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0}}],\"service_name\":\"CNA\",\"service_provider\":\"mediacorp\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":1840,"wires":[["412aeca37583d06d"]]},{"id":"412aeca37583d06d","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload ~> |streams| {\"langauage\": $.tags.language}, [\"tags\"]|\t\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":1840,"wires":[["cf16e07496d3f02d"]]},{"id":"cf16e07496d3f02d","type":"debug","z":"bf9e1e33.030598","name":"Issue is here","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":1840,"wires":[]}]

```

[Transform](https://docs.jsonata.org/other-operators#-------transform)

```auto
$$.payload ~> |streams| {"langauage": $.tags.language}, ["tags"]|		

```

Or low code option

```auto
[{"id":"a9348d92797fed33","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"program_id\":16,\"program_num\":16,\"pcr_pid\":620,\"streams\":[{\"codec_type\":\"video\",\"id\":\"0x26c\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0}},{\"codec_type\":\"audio\",\"id\":\"0x277\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"eng\"}},{\"codec_type\":\"audio\",\"id\":\"0x278\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"qab\"}},{\"codec_type\":\"subtitle\",\"id\":\"0x280\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"eng\"}},{\"codec_type\":\"subtitle\",\"id\":\"0x281\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"zho\"}},{\"codec_type\":\"subtitle\",\"id\":\"0x282\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"msa\"}},{\"codec_type\":\"subtitle\",\"id\":\"0x283\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0},\"tags\":{\"language\":\"tam\"}},{\"id\":\"0x2a8\",\"disposition\":{\"default\":0,\"dub\":0,\"original\":0,\"comment\":0,\"lyrics\":0,\"karaoke\":0,\"forced\":0,\"hearing_impaired\":0,\"visual_impaired\":0,\"clean_effects\":0,\"attached_pic\":0,\"timed_thumbnails\":0,\"captions\":0,\"descriptions\":0,\"metadata\":0,\"dependent\":0,\"still_image\":0}}],\"service_name\":\"CNA\",\"service_provider\":\"mediacorp\"}","payloadType":"json","x":190,"y":1920,"wires":[["e353ab05c11e2d03"]]},{"id":"e353ab05c11e2d03","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"hold","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"hold.streams","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":1840,"wires":[["754baad4.93518c"]]},{"id":"754baad4.93518c","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":380,"y":1880,"wires":[["412aeca37583d06d"]]},{"id":"412aeca37583d06d","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload.language","pt":"msg","to":"payload.tags.language","tot":"msg"},{"t":"delete","p":"payload.tags","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":1940,"wires":[["bc021cb3.4cb958"]]},{"id":"bc021cb3.4cb958","type":"join","z":"bf9e1e33.030598","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":660,"y":1900,"wires":[["deeae6d0.1c813"]]},{"id":"deeae6d0.1c813","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"hold.streams","pt":"msg","to":"payload","tot":"msg"},{"t":"move","p":"hold","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":1880,"wires":[["cf16e07496d3f02d"]]},{"id":"cf16e07496d3f02d","type":"debug","z":"bf9e1e33.030598","name":"Issue is here","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":1820,"wires":[]}]

```

---

<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:** [21 May 2022 11:54 UTC](https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907/7 "2022-05-21T11:54:34Z")

</div>

Thank you so much, @E1cid !

I wasn't aware about the transform function, I will have a look at it. Thanks a lot !

---

<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 June 2022 11:55 UTC](https://discourse.nodered.org/t/how-to-use-access-a-message-variable-to-navigate-an-array-with-a-jsonata-expression-in-a-switch-node/62907/8 "2022-06-04T11:55:21Z")

</div>

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