Yes, JSONata is a very different language, and the referencing syntax for object fields with special characters uses quotes and not [ ]
.
You want
payload.'weather.forecast_home'.forecast[0].precipitation
where the key with the problematic .
in it is “weather.forecast_home” .
For anyone who likes the ‘documentation - approach’ to learning new things, the official JSONata documentation says to use backticks ` to enclose key fields with special characters, but I am finding that ordinary single quotes are working just as well.
The ‘path reference’ in the Node-RED debug window uses the JavaScript referencing approach, but in JSONata the [ ]
is a special filter operator, so in using this you are actually asking for msg.payload to be filtered by the expression “weather.forecast_home” being true
which it is not, hence no output.
Since this question does come up from time to time, particularly with the HA use of “.” in the middle of key fields, here is how to debug JSONata and test this for yourself.
First capture the full message output from the previous node, so that you can copy the entire message object in the NR debug window.
Then go to https://try.jsonata.org/
In the left hand window, select the entire JSON input object (example) and paste over this with your debug message output.
You can now use the top right hand window to write a JSONata expression, and the evaluation result appears in the window below. This is dynamic, so typing in
payload
gets the msg.payload object, then you can continue the expression.
Note that the left hand side can be edited too, which makes debugging easier.
As a bonus, since the problem is the key with the “.” in it, you can also use the JSONata wildcard operator. Since there is only one forecast return from the service call
payload.*.forecast[0].precipitation
will return just the forecast object we want. Even better
payload.**.precipitation
will return an array of all precipitation values, using an ‘any-depth’ wildcard. This means that you can also use
(payload.**.precipitation)[0]
although the required use of ( )
is another oddity of JSONata since the [ ]
filter operator binds more tightly to the precipitation field than it does to the output of payload.**.precipitation