How to extract data from an array?

Hey guys! I have a flow where it returns information for each Whatspp contact and returns the following JSON:

[
{
“communityId”: null,
“isGroupAnnouncement”: false,
“isGroup”: false,
“name”: “Stefanie”,
“phone”: “554999222019”,
“unread”: “0”,
“lastMessageTime”: “undefined”,
“isMuted”: “0”,
“agentId”: null,
“tags”: {
“3”: true
},
“archived”: “false”,
“pinned”: “false”,
“muteEndTime”: null,
“profileThumbnail”: null,
“ephemeralExpiration”: 0,
“messagesUnread”: 0,
“about”: “Disponível”
}
]

The TAG item returns a customer’s tags. I need to create a flow to separate the flow according to the customer tag. To do this, I need to treat this JSON to return as follows:

“tag”: “3”

Remembering that the client can have more than one TAG (ex: 3, 5, 7) how could I treat the array to extract the data this way?

  • n8n version: 1.25.1
  • Database: PostgreSQL
  • *n8n EXECUTIONS_PROCESS setting: Main
  • Running n8n via: Docker Swarm
  • Operating system: Linux Debian 11

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

@rfdepauli , just to understand you clearer, how does tag look like if there are more than one?

From your example, when there is only one tag, you need to transform

    "tags": {
      "3": true
    },

into

    "tags": "3",

Does multi-tag item has an array or a dictionary? That is, it is like this

    "tags": {
      "3": true
      "5": true
    },

or like below?

    "tags": [
      {
          "3": true
      },
      {
        "5": true
      }
    ],

If there are more than one, you want to replicate the data but have only one tag alongside it so that you have something like below?

[
  {
    . . .
    "tags": "3",
    . . .
  },
  {
    . . .
    "tags": "5",
    . . .
  },
  . . .
]
1 Like

Hello Ihortom! Thanks for the support!

It would be similar to your example 2, that is, when there is more than one tag the return is as follows:

[
{
“communityId”: null,
“isGroupAnnouncement”: false,
“isGroup”: false,
“name”: “049991605767”,
“phone”: “554991605767”,
“unread”: “0”,
“lastMessageTime”: “1706186455000”,
“isMuted”: “0”,
“agentId”: null,
“tags”: {
“1”: true,
“2”: true,
“3”: true,
“4”: true,
“6”: true,
“7”: true,
“8”: true,
“9”: true,
“14”: true,
“15”: true
},
“archived”: “false”,
“pinned”: “false”,
“muteEndTime”: null,
“profileThumbnail”: “https://pps.whatsapp.net/v/t61.24694-24/371302017_330943346026458_6255855031565326650_n.jpg?ccb=11-4&oh=01_AdRb0dco1wr35OAUrwoBZ5HAvqlGP5KJJ_wzH1SmWwp1Qw&oe=65BF6E25&_nc_sid=e6ed6c&_nc_cat=110”,
“ephemeralExpiration”: 0,
“messagesUnread”: 0,
“about”: “Nada em excesso, incluindo moderação! :sunglasses:
}
]

However, evaluating the flow sequence and imagining that I will have to make a condition on the next node, I may have to change the return. For example, if I use an IF node with the condition CONTAINS the number 1, the flow can be triggered when there is the number 1, but also with the number 10. Therefore, I believe that the most correct option is to return the data in the following way:

  "tag1": true,
  "tag2": true,
  "tag3": true,
  "tag4": true,
  "tag6": true,
  "tag7": true,
  "tag8": true,
  "tag9": true,
  "tag14": true,
  "tag15": true

An important issue is that in this API not all will always appear in the output. Furthermore, they may eventually appear as TRUE or FALSE.

@rfdepauli , it sounds like you are open to a different solution as long as you can easily validate a specific tag is present and and it has value true.

I’m offering you the following transformation. Assuming you have something like this (note occasional false to cater for that possibility too)

    "tags": {
      "1": true,
      "2": false,
      "3": true,
      "4": true,
      "6": true,
      "7": true,
      "8": false,
      "9": true,
      "14": false,
      "15": true
    }

then it could be easily transformed into

{
  "tags": [
    1,
    3,
    4,
    6,
    7,
    9,
    15
  ]
}

That is, you have an arrays (not an object), the tag names converted to integers, and those with value of false are filtered out.

Then you can verify the presence of a tag with $json.tags.includes(NUMBER).

If that sounds good to you, here’s how the transformation could be achieved:

Hello Ihortom! Thanks again for your feedback!

Yes, I’m open to new solutions, thank you very much!

TRUE means new tags are being added and FALSE means they have been removed. Therefore, I need to map the FALSE data as well.

How could I modify the function below to only filter false tags this time?

{{ Object.entries($json.tags).filter(i => i[1]).map(i => parseInt(i[0])) }}

@rfdepauli , the part responsible for filtering out is .filter(i => i[1]). To revert it to filter out all the “trues”, you just need to negate the condition, .filter(i => !i[1]) (note a !). Thus, the rewritten expression is

{{ Object.entries($json.tags).filter(i => !i[1]).map(i => parseInt(i[0])) }}

This will leave you all the tags that have Boolean false value.

In case the tags are not necessarily have integer equivalent, you can remove parseInt() function used in map(). That is, transform .map(i => parseInt(i[0])) into .map(i => i[0]).

Hello Ihortom!

It worked perfectly here. However, I am subsequently evaluating the returned results and the Switch node is always activating output 1, even when it should activate another output. I believe you’re not doing the condition correctly, right?!

@rfdepauli ,

I believe you’re not doing the condition correctly, right?!

:slight_smile:
It doesn’t work because you are comparing an array with a string. To fix, pick “contains” from Array, not String, and replace the string value “NUMBER” to just NUMBER.

Oh! True, I didn’t notice that!

But I corrected it here, but even so the flow always goes through output 1!

@rfdepauli , this is because you have only one item, you need to duplicate them by the number of tags as I mentioned at the very first comment of mine. Do you need help with that?

Please! I don’t quite understand this yet!

@rfdepauli , it’s actually pretty simple. Just add “Split Out” node.

Note that now instead of 1 item with 7 tags that have value true you have 7 items with only one tag - for each of the 7. As a result, the array is also gone. Thus, your Switch has to compare numbers (no more array).

1 Like

Thank you very much, Ihortom!

Worked perfectly!

Thank you for all the support and support!

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