How to Find Unique Names from Input Using DataWeave

This article will help you practice your DataWeave skills in MuleSoft. Here we will find unique names from our Input. Let’s get started.

Input:

[
    [
        {
            "name":"john"
        },
        {
            "name":"leonardo"
        }
    ],
    [
        {
            "name": "leonardo"
        },
        {
            "name": "alicia"
        },
        {
            "name": "jennifer"
        },
        {
            "name": "john"
        }
    ]
]

Expected Output:

[
  "john",
  "leonardo",
  "alicia",
  "jennifer"
]

Let’s talk about the solution now. We are going to achieve this in multiple steps as follows:

Step 1:

This step will generate output like below-

[
  "john",
  "leonardo",
  "leonardo",
  "alicia",
  "jennifer",
  "john"
]
Dataweave Code:
%dw 2.0
output application/json
---
flatten(payload map ((item, index) -> item.name))
Step 2:

This step will generate the final expected output by using below dataweave code –

%dw 2.0
output application/json
---
flatten(payload map ((item, index) -> item.name)) distinctBy ((item, index) -> item)

Hope this helps improve your DataWeave skills. Thanks..!!