Retrieve Unique Records In Dataweave 2.0

 

In this tutorial we will demonstrate how You can use distinctBy function to Retrieve Unique Records In Dataweave 2.0 from an array ,distinctBy can also be used to remove the duplicate key/value pair from object.

Example 1:

Input:

["test", "test", "test1", "test2", "test1", "test3", "test3", "test4"]

Dataweave Expression:

%dw 2.0
output application/json
---
payload distinctBy (value) -> { "unique" : value }

Output:

[
  "test",
  "test1",
  "test2",
  "test3",
  "test4"
]

Example 2:

Input:

{
  "employee_name": "John",
  "employeeid": "123",
  "employee_designation": [
    "TA",
    "SSE",
    "SE",
    "SE",
    "PM",
    "TA"
  ]
}

Dataweave Expression:

%dw 2.0
output application/json
---
{
  employeename:payload.employee_name,
  employeeid:payload.employeeid,
  designation:payload.employee_designation distinctBy $
}

Output:

{
  "employeename": "John",
  "employeeid": "123",
  "designation": [
    "TA",
    "SSE",
    "SE",
    "PM"
  ]
}
  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments