Node-Red – Using MSG fields on JSONata expression

I’m using Node-Red.
I have set a field named “counter” for msg object, I want to use it’s value in an JSONata expression, the expression is this:

$keys(payload)[counter]

Consider this payload:

{
    "counter": 0,
    "payload": {"cv": 1, "fg": 2}
}

When I test it, I get this result:

enter image description here

When I enter 0 directly instead of using counter, it shows the correct result and when I just use “counter” as expression, it evaluate it as 0

enter image description here

enter image description here

Why the main expression is not evaluating correctly?

How should I write it?

Read more here: Source link