How to return length of empty item as 0?

Describe the issue/error/question

I’m using “Always output data” to ensure a Split In Batches loop doesn’t break.

However, I also need to count the output items of one of my SQL queries.

The issue with using {{$items().length}} in this case is that it counts the empty item as 1 instead of 0 (this is expected, since we are measuring by the number of items).

I tried using an IF node with the expression {{Object.keys($items()[0].json).length === 0}} so I can use a pair of SET nodes to conditionally set the count field to 0 however the names and lengths don’t align with their corresponding queries: Client A → shows length of Client B, etc.

How do I get the correct output of ‘0’ without mixing my data around? (both examples shown)

What is the error message (if any)?

Please share the workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 0.200.1
  • Database you’re using (default: SQLite): Postgres
  • Running n8n with the execution process [own(default), main]: own
  • Running n8n via [Docker, npm,, desktop app]: K8s

You could try a .filter on the array where it’s not an empty object, or the object has > 0 keys. Something like
items.filter(x => Object.keys(x).length).length

I don’t think that will work in an expression through because of the arrow function.

1 Like

Thanks I tried that in a code node but it always returns an empty value:

const items = $items("Get All Tickets > 3 Months")
return [items.filter(x => Object.keys(x).length).length]

I tested using return [items] and the first line does output the contents of that node, so I know that part is correct.

Not sure what I’m doing wrong, probably something obvious but javascript is not my strong suit

Hi there - I did not find a solution for the length issue but in the end I ended up solving my problem with a couple of JOIN statements in my SQL query:

SELECT ticket.number AS ticket_number, ticket.user_id, closed, ticket.created, ticket.updated,, user.org_id, AS org_name
FROM osti_ticket AS ticket
INNER JOIN osti_user AS user
ON user_id =
INNER JOIN osti_organization AS org
ON org_id =
WHERE ticket.created > '{{ $item("0").$node["Date & Time"].json["date"] }}' AND user_id = {{$json["user_id"]}}

I think the misalignment of data was happening on the second IF node because the number of records doesn’t match (in other words the name exists but the ticket item does not).

For future reference it probably needed to be Object.keys(x.json). That one still gets me.

1 Like

I found an even better solution!

TIL you can use runIndex to get the execution index of any node!

Using this method, my original, more lightweight SQL query can still be used AND it’s a more ‘n8nonic’ way of achieving this I think :smiley:

{{$items("Get Organizations")[$node["IF"].runIndex].json['org_name']}}

1 Like

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