How to loop in an array

Hi, I have this output from a node:

[
{
"data": [
    {
    "label": "n8n"
    },
    {
    "label": "important"
    }
]
}
]

How can I iterate in the “data” array ? I need to check if (for each element of the data array) a GMAIL label with “label” name exist

I thought I had to use the loop node but I don’t how to use it for this scope.

Thanks in advance.

Information on your n8n setup

  • n8n version:. latest
  • Database (default: SQLite):. auto
  • n8n EXECUTIONS_PROCESS setting (default: own, main):. own
  • Running n8n via (Docker, npm, n8n cloud, desktop app):. npm on vps
  • Operating system: Debian 12

you can try one of these:

or

if this is what you looking for please don’t forget to mark the answer above as the Solution

@Michel_Morelli you may want to give the following solution a shot.

The IF node uses the following expression (explained below) to extract all labels from the input:
{{ $json.data.map(element => element.label) }}

$json.data refers to the data array.
.map goes through each element ({ label: "abcd" }) in the array and transforms it using the callback function.
element => element.label function returns value of label property of an element.

Next we need to configure the IF node to work with expression’s output properly. The expression returns an array of string and we want to know if this array contains specific value.

I must admit that the first solution from @mohamed3nan might be more obvious.

@mohamed3nan , I am not criticising your solutions, they are completely legit and straightforward. In fact, a couple of days ago I myself would go for Code node. Full controll, all-in-one, you know. Just started digging into peculiarities of data transformation and core n8n nodes. n8n is fabulous.

Back to the Code node - I assume Code nodes execution is more resource-costly than expressions, so I am trying to avoid it wherever the same result can be achieved with less “heavy” nodes.

@Michel_Morelli , pick the solution you think would be easier for you to maintain and build on.

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