# Max "Array Number" in a msg.payload

**URL:** <https://discourse.nodered.org/t/max-array-number-in-a-msg-payload/48219>\
**Category:** General\
**Created:** [9 July 2021 18:25 UTC](https://discourse.nodered.org/t/max-array-number-in-a-msg-payload/48219 "2021-07-09T18:25:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![finch6](https://avatars.discourse-cdn.com/v4/letter/f/9d8465/32.png) [@finch6](https://discourse.nodered.org/u/finch6)\
**Post date:** [9 July 2021 18:25 UTC](https://discourse.nodered.org/t/max-array-number-in-a-msg-payload/48219/1 "2021-07-09T18:25:00Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/40327bd17d7d34bb35b7df233e25b2c3e6de622d.png)

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!!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 July 2021 19:11 UTC](https://discourse.nodered.org/t/max-array-number-in-a-msg-payload/48219/2 "2021-07-09T19:11:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [9 July 2021 19:40 UTC](https://discourse.nodered.org/t/max-array-number-in-a-msg-payload/48219/3 "2021-07-09T19:40:01Z")

</div>

@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];`

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 July 2021 19:44 UTC](https://discourse.nodered.org/t/max-array-number-in-a-msg-payload/48219/4 "2021-07-09T19:44:57Z")

</div>

Cheers, slipped my mind there. Corrected post

---

<div class="post-metadata">

**Author:** ![finch6](https://avatars.discourse-cdn.com/v4/letter/f/9d8465/32.png) [@finch6](https://discourse.nodered.org/u/finch6)\
**Post date:** [10 July 2021 09:42 UTC](https://discourse.nodered.org/t/max-array-number-in-a-msg-payload/48219/5 "2021-07-10T09:42:06Z")

</div>

Thanks you so much @E1cid and @UnborN - really appreciate the help!  
I'll give that a bash 👍🏼

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 September 2021 09:42 UTC](https://discourse.nodered.org/t/max-array-number-in-a-msg-payload/48219/6 "2021-09-08T09:42:51Z")

</div>

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