Identify duplicates in items

Hello everyone,
I would like to check and get the item with the same value.
In this case i want to take the contact with the same EMAIL_CONTACT

[
  {
    "N_DE_COMMANDE": "AB123456",
    "DATE_EXPIRATION": "11/9/2022",
    "ADRESSE": "18 place de l'iris",
    "EMAIL_CONTACT": "[email protected]",
    "Today Date": "12/8/2022"
  },
  {
    "N_DE_COMMANDE": "71AN71",
    "DATE_EXPIRATION": "11/9/2022",
    "ADRESSE": "123 place de l'iris",
    "EMAIL_CONTACT": "[email protected]",
    "Today Date": "12/8/2022"
  },
  {
    "N_DE_COMMANDE": "52640GT-HG",
    "DATE_EXPIRATION": "10/5/2022",
    "ADRESSE": "21 place de l'iris",
    "EMAIL_CONTACT": "[email protected]",
    "Today Date": "12/8/2022"
  },
  {
    "N_DE_COMMANDE": "AZERTY 02",
    "DATE_EXPIRATION": "12/1/2022",
    "ADRESSE": "22 place de l'iris",
    "EMAIL_CONTACT": "[email protected]",
    "Today Date": "12/8/2022"
  }
]

This is the output of the node before the check.
Do you have any suggestion?

Hi @Gabriele_Bracciali, I think the most readable way in n8n would be to use the Item Lists node to get the unique values from the EMAIL_CONTACT field, then a Set node using a bit of JMESPath logic to find the matching items from a previous node.

Like so:

This would be the result:

The full expression I’ve used here is {{ $jmespath($('Mock data').all(), "[?json.EMAIL_CONTACT=='" + $json.EMAIL_CONTACT + "'].json") }}, which might looks a bit weird at first. So let me try to break this down:

  • $jmespath(object, searchString) is the method used in n8n to access JMESPath. It requires one object (to which the filtering will be applied) and one string (which describes the filter).
  • $('Mock data').all() is our object and would simply be a method to retrieve all items (not just one) from the Mock data node in the example workflow.
  • "[?json.EMAIL_CONTACT=='" + $json.EMAIL_CONTACT + "'].json" is the string consisting of three parts joined using +. The middle part $json.EMAIL_CONTACT would read the value of the EMAIL_CONTACT for the current item (coming from our email lists node). For a value of [email protected], the full string would be "[?json.EMAIL_CONTACT=='[email protected]'].json". This is what JMESPath calls a filter projection and is explained here in their docs.

Hope this is roughly what you had in mind. Let me know if you have any questions on this :slight_smile:

2 Likes

Hello @MutedJam ,
thank for helping me.

Yes this is what i need, perfect solution!

1 Like

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