# Getting Last Object payload for an array that varies

**URL:** <https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032>\
**Category:** General\
**Created:** [16 October 2018 15:44 UTC](https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032 "2018-10-16T15:44:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![2004sk4](https://avatars.discourse-cdn.com/v4/letter/2/e68b1a/32.png) [@2004sk4](https://discourse.nodered.org/u/2004sk4)\
**Post date:** [16 October 2018 15:44 UTC](https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032/1 "2018-10-16T15:44:23Z")

</div>

What is the best way to get the last object payload from a sql array.

I am calling a sql server which depending on datatime pulls different amount a results. I need that last object payload to display on the dashboard

I would like to get the information from object 42 during this example.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3cdf1e6edaeb89e2b05bf3fd4742672f94ea1b9c.png)

How can i adjust [0] in below example.  
{{msg.payload[0].EmployeeNumber}} for the dashboard

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [16 October 2018 16:10 UTC](https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032/2 "2018-10-16T16:10:44Z")

</div>

The method `length` will indicate the array size. The last element is array size -1 , so you could try using:

{{msg.payload[msg.payload.length-1].EmployeeNumber}}

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 October 2018 16:52 UTC](https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032/3 "2018-10-16T16:52:27Z")

</div>

Alternatively if you are not using the earlier ones at all then change the SQL query so it only returns the last one.  
Alternatively again you could change the sort order on the query to return them in the opposite order then it would always be [0].

---

<div class="post-metadata">

**Author:** ![2004sk4](https://avatars.discourse-cdn.com/v4/letter/2/e68b1a/32.png) [@2004sk4](https://discourse.nodered.org/u/2004sk4)\
**Post date:** [17 October 2018 12:49 UTC](https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032/4 "2018-10-17T12:49:40Z")

</div>

That solution worked great.

---

<div class="post-metadata">

**Author:** ![2004sk4](https://avatars.discourse-cdn.com/v4/letter/2/e68b1a/32.png) [@2004sk4](https://discourse.nodered.org/u/2004sk4)\
**Post date:** [17 October 2018 12:55 UTC](https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032/5 "2018-10-17T12:55:32Z")

</div>

The only option I was giving by the person who set the database up was to exec with the start and end date/time. I needed to read the last person thru the gate to display some information

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 October 2018 13:19 UTC](https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032/6 "2018-10-17T13:19:15Z")

</div>

Not much of a database if you can't query for the last record. Still, you have a solution so that is the main thing.

---

<div class="post-metadata">

**Author:** ![umtksa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umtksa/32/38322_2.png) [@umtksa](https://discourse.nodered.org/u/umtksa)\
**Post date:** [12 September 2023 23:43 UTC](https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032/7 "2023-09-12T23:43:32Z")

</div>

thanks againg 🖖

---

<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:** [26 September 2023 23:43 UTC](https://discourse.nodered.org/t/getting-last-object-payload-for-an-array-that-varies/4032/8 "2023-09-26T23:43:47Z")

</div>

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