Help JSONata Expression

I need help with this JSONata Expression. It's not working...
image

Well I solved it already.

(
    $x := msg.idx;
    msg.payload[$x];
)

Alternatively, msg.payload[$$.msg.idx]. As a side note, you should not be referring to msg, which is why you're getting the "Compatibility mode enabled" message (click the "i" to see why). Therefore it would be payload[$$.idx].

1 Like

msg.payload is also equivalent to $$.payload ?

Normally you don't need to specify the root node, so payload and $$.payload are the same thing. JSONata has the concept of a context, where $ is the current context and $$ is the root. Further info can be found here.

Once you specify payload, anything under that is within the context of payload. So payload[idx] is possibly handled as a predicate, which would normally be along the lines of payload[idx=1] where it's looking for an element of payload called idx with a value of 1. This would work with the following json:

{
  "payload": [
    { "idx": 1 },
    { "idx": 2 }
  ]
}

JSONata does my head in pretty quickly because it's quite powerful and very succinct.

I'm guessing the change mentioned in "compatibility mode" is the root node used to be the document rather than msg. In which case $$.something might break other parts of NR. Making msg the root means you can't "break out" to other parts that NR might use for other purposes. Perhaps?

4 Likes