DataWeave Interview Question: Find Unique Names From the Input
A quick tutorial article that shows you the code you need to better work with Dataweave and Mulesoft, focusing on a common interview question.
Join the DZone community and get the full member experience.
Join For FreeThis article will help you practice your DataWeave skills in MuleSoft. Here we will find unique names from our Input. Let's get started.
Input:
x
[[
{
"name":"john"
},
{
"name":"leonardo"
}
],
[
{
"name": "leonardo"
},
{
"name": "alicia"
},
{
"name": "jennifer"
},
{
"name": "john"
}
]]
Output:
xxxxxxxxxx
[
"john",
"leonardo",
"alicia",
"jennifer"
]
Let's talk about the solution now. We are going to achieve this in multiple steps as follows:
Step 1 Output:
xxxxxxxxxx
[
"john",
"leonardo",
"leonardo",
"alicia",
"jennifer",
"john"
]
In order to get output like this, we will use below code:
xxxxxxxxxx
%dw 2.0
output application/json
---
flatten(payload map ((item, index) -> item.name))
Step 2 will have the final output. We will use the below code to achieve the same.
xxxxxxxxxx
%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!
Opinions expressed by DZone contributors are their own.
Comments