Max "Array Number" in a msg.payload

Hi all,

I'm fairly new to Node-Red, so please forgive me incorrectly naming things if I do...

I'm trying to use Node-Red to pull data from a Corona API and push this to a telegram bot.
I have managed to set up the Telegram Bot but am struggling with the data from the API.

The msg.payload for the daily cases is added in an array (I think). I assume it's an array because it says array[494] in the right pane.

Screenshot 2021-07-09 at 20.02.43

So currently we are sitting on payload.National.Cases[493], yesterday was payload.National.Cases[492].

I'm trying to use a Template Node to use payload.National.Cases[x] where x is the largest number which should give me the latest data. How do I do this and with what nodes??

It's not only the payload.National.Cases that I will be using, but all of the objects have the same arrays, so if I can get this one correct, I can apply this logic to the other objects.

Thanks in advance everyone!

Stay safe out there!!

using change node set to JSONata, set var name to expression
payload.National.Cases[-1] would give you the last element in an array.

or in a function node
msg.varname = msg.payload.National.Cases[msg.payload.National.Cases.length-1]; would do the same.

you would then use that var in the template node
[edit] Corrected error.

@E1cid for the function node solution there is a typo .. you need to subtract 1 from length
msg.varname = msg.payload.National.Cases[msg.payload.National.Cases.length - 1];

Cheers, slipped my mind there. Corrected post

Thanks you so much @E1cid and @UnborN - really appreciate the help!
I'll give that a bash :+1:t3:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.