Getting Last Object payload for an array that varies

#1

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

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

0 Likes

#2

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

0 Likes

#3

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].

1 Like

#4

That solution worked great.

1 Like

#5

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

0 Likes

#6

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.

1 Like