# \[Solved\] Jsonata in a change node: Can you use a msg variable for an array index?

**URL:** https://discourse.nodered.org/t/solved-jsonata-in-a-change-node-can-you-use-a-msg-variable-for-an-array-index/13529
**Category:** General
**Created:** [22 July 2019 00:37 UTC](https://discourse.nodered.org/t/solved-jsonata-in-a-change-node-can-you-use-a-msg-variable-for-an-array-index/13529 "2019-07-22T00:37:36Z")
**Posts on this page:** 7
**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: [22 July 2019 00:37 UTC](https://discourse.nodered.org/t/solved-jsonata-in-a-change-node-can-you-use-a-msg-variable-for-an-array-index/13529/1 "2019-07-22T00:37:36Z")

</div>

I know I can do this in a function node, but I want to figure out how do do this in a change node with jsonata. Here is the msg:

```auto
{"payload":0,
 "socketid":"g7R4DAwyPG3jLyHKAACQ",
 "_msgid":"2c51e9cf.94bd06",
 "path":"/Users/Paul/ESPBRR",
 "files":["coffee30_backups","node21_backups","node29_backups"]
}

```

What I want to do is use the value of `payload` be the index into `files`

In jsonata I can enter `payload` and it returns `0`  
I can enter files[0] and it returns `coffee30_backups`  
but if I use `files[payload]` (in the jsonata test section) I get `No matching result`

Is it possible to do? Here is a test flow

```auto
[{"id":"7371d95.899e6a8","type":"debug","z":"75f6583c.143608","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":450,"y":360,"wires":[]},{"id":"a5100f28.c5ac48","type":"inject","z":"75f6583c.143608","name":"","topic":"","payload":"[\"coffee30_backups\",\"node21_backups\",\"node29_backups\"]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":360,"wires":[["2345278.426d5d8"]]},{"id":"2345278.426d5d8","type":"change","z":"75f6583c.143608","name":"Setup msg object","rules":[{"t":"set","p":"files","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"},{"t":"set","p":"selection","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":360,"wires":[["7371d95.899e6a8","30505485.3f0bac"]]},{"id":"30505485.3f0bac","type":"change","z":"75f6583c.143608","name":"using files[payload]","rules":[{"t":"set","p":"selection","pt":"msg","to":"files[payload]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":420,"wires":[["40ff4ef6.d236f"]]},{"id":"40ff4ef6.d236f","type":"debug","z":"75f6583c.143608","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":450,"y":420,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [22 July 2019 06:51 UTC](https://discourse.nodered.org/t/solved-jsonata-in-a-change-node-can-you-use-a-msg-variable-for-an-array-index/13529/2 "2019-07-22T06:51:31Z")

</div>

Try `files[$.payload]`

---

<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: [22 July 2019 08:58 UTC](https://discourse.nodered.org/t/solved-jsonata-in-a-change-node-can-you-use-a-msg-variable-for-an-array-index/13529/3 "2019-07-22T08:58:19Z")

</div>

> [@knolleary](#):
>
> files[$.payload]

Ohh I was so hopeful, but that doesn't work eithor ☹

---

<div class="post-metadata">

### Author: ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)
#### Post date: [22 July 2019 10:27 UTC](https://discourse.nodered.org/t/solved-jsonata-in-a-change-node-can-you-use-a-msg-variable-for-an-array-index/13529/4 "2019-07-22T10:27:06Z")

</div>

it is

```auto
files[$$.payload]
```

---

<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: [22 July 2019 10:52 UTC](https://discourse.nodered.org/t/solved-jsonata-in-a-change-node-can-you-use-a-msg-variable-for-an-array-index/13529/5 "2019-07-22T10:52:26Z")

</div>

@moebius Walter nice! That works. Now where did you fine that? I've been searching all over

---

<div class="post-metadata">

### Author: ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)
#### Post date: [22 July 2019 11:17 UTC](https://discourse.nodered.org/t/solved-jsonata-in-a-change-node-can-you-use-a-msg-variable-for-an-array-index/13529/6 "2019-07-22T11:17:07Z")

</div>

[https://www.google.com/search?q=jsonata+array+dynamically](https://www.google.com/search?q=jsonata+array+dynamically)

edit: lmgtfy shows different results....first should be:

> <https://github.com/jsonata-js/jsonata/issues/323>

---

<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: [22 July 2019 11:41 UTC](https://discourse.nodered.org/t/solved-jsonata-in-a-change-node-can-you-use-a-msg-variable-for-an-array-index/13529/7 "2019-07-22T11:41:41Z")

</div>

> - `$$` The root of the input JSON. Only needed if you need to break out of the current context to temporarily navigate down a different path. E.g. for cross-referencing or joining data.  
> from [Programming constructs · JSONata](https://docs.jsonata.org/programming#variables)

Ahhh, I missed that when reading all those docs. Thanks!
