Filter In Dataweave

 

Filter in dataweave used to return the matching values as per the expression applied.

I will use the below input and filter the employee on as per there salary and age

Input:

{
	"status": "success",
	"data": [
		{
			"id": "1",
			"employee_name": "Tiger Nixon",
			"employee_salary": 320800,
			"employee_age": 61,
			"profile_image": ""
		},
		{
			"id": "2",
			"employee_name": "Garrett Winters",
			"employee_salary": 170750,
			"employee_age": 63,
			"profile_image": ""
		},
		{
			"id": "3",
			"employee_name": "Ashton Cox",
			"employee_salary": 86000,
			"employee_age": 66,
			"profile_image": ""
		},
		{
			"id": "4",
			"employee_name": "Cedric Kelly",
			"employee_salary": 433060,
			"employee_age": 22,
			"profile_image": ""
		},
		{
			"id": "5",
			"employee_name": "Airi Satou",
			"employee_salary": 162700,
			"employee_age": 33,
			"profile_image": ""
		},
		{
			"id": "6",
			"employee_name": "Brielle Williamson",
			"employee_salary": 372000,
			"employee_age": 61,
			"profile_image": ""
		}
	]
}

Dataweave Expression:

%dw 2.0
output application/json
---
payload.data filter ($.employee_salary > 300000 and $.employee_age < 60 ) map((item,index) ->
{
	"id":item.id,
	"employee_name":item.employee_name,
	"employee_salary":item.employee_salary,
	"employee_age":item.employee_age,
	"profile_image":item.profile_image
})

Output:

[
  {
    "id": "4",
    "employee_name": "Cedric Kelly",
    "employee_salary": 433060,
    "employee_age": 22,
    "profile_image": ""
  }
]
  
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
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
shreyas
shreyas
2 years ago

How can i get the data which is filtered out , if i filter 100 records , if i just have 90 records after filtering, is the data of the other 10 records lost? is there anyway to handle them during filter.

Gourav
Gourav
1 year ago

hi

R Palani
R Palani
1 month ago

%dw 2.0
output application/json

payload.data filter($.employee_name == “Cedric Kelly”) map{
“id”: $.id,
“employee_name”: $.employee_name,
“employee_salary”: $.employee_salary,
“employee_age”: $.employee_age,
“profile_image”: $.profile_image
}