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

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:

{"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

[{"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":[]}]

Try files[$.payload]

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

it is

files[$$.payload]
1 Like

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

https://www.google.com/search?q=jsonata+array+dynamically

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

  • $$ 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

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