DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Querying Without a Query Language
  • Tableau Dashboard Development Best Practices
  • An Introduction to Bloom Filters
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases

Trending

  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  • Architecting Sub-Microsecond HFT Systems With C++ and Zero-Copy IPC
  • Java in a Container: Efficient Development and Deployment With Docker
  • The Vector Database Lie

Dataweave Filter Function

I wanted to take this opportunity to share my experience with filters with you — how can we use the filter function in some complex scenarios.

By 
Radhika A user avatar
Radhika A
·
Nov. 11, 20 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
25.8K Views

Join the DZone community and get the full member experience.

Join For Free

Surprisingly there are very few examples of the filter function of Dataweave in MuleSoft. I wanted to take this opportunity to share my experience with filters with you. The following example shows how can we use the filter function in some complex scenarios.

The filter function in the dataweave is used to filter the values based on matching expression. The expression returns the boolean values either true or false. This function can be applied to an array. It iterates the array and matches the values with the expression. The output of the filter function is an array with the matching values.

Scenario #1: Here the example shows how to filter an xml based on arguments of an element. The below dataweave code shows how can we achieve this if the output is in JSON.

Input:

Java
 




x
45


 
1
<?xml version="1.0" encoding="UTF-8"?>
2

          
3
<EmployeeDetails>
4

          
5
   <Data type="Accountant Department">
6

          
7
      <Employee>
8

          
9
         <Name>John</Name>
10

          
11
         <Designation>Accountant</Designation>
12

          
13
      </Employee>
14

          
15
      <Employee>
16

          
17
         <Name>Kevin</Name>
18

          
19
         <Designation>Consultant</Designation>
20

          
21
      </Employee>
22

          
23
   </Data>
24

          
25
   <Data type="Teaching Department">
26

          
27
      <Employee>
28

          
29
         <Name>Jolly</Name>
30

          
31
         <Designation>Science Teacher</Designation>
32

          
33
      </Employee>
34

          
35
      <Employee>
36

          
37
         <Name>Chrissy</Name>
38

          
39
         <Designation>Maths Teacher</Designation>
40

          
41
      </Employee>
42

          
43
   </Data>
44

          
45
</EmployeeDetails>



From the above payload if we want to filter the data based on type.

DataWeave Code:

%dw 2.0

Output application/json

Payload.EmployeeDetails.*Data filter($.@'type'=="Teaching Department")

Output:

Java
 




xxxxxxxxxx
1
24


 
1
[
2

          
3
  {
4

          
5
    "Employee": {
6

          
7
      "Name": "Jolly",
8

          
9
      "Designation": "Science Teacher"
10

          
11
    },
12

          
13
    "Employee": {
14

          
15
      "Name": "Chrissy",
16

          
17
      "Designation": "Maths Teacher"
18

          
19
    }
20

          
21
  }
22

          
23
]
24

          



The below dataweave code shows how can we filter the data and the output is in xml.

DataWeave Code:

%dw 2.0

Output application/xml

DepartmentType: payload.EmployeeDetails.*Data filter($.@'type'=="School Teaching Department")

Output:

Java
 




xxxxxxxxxx
1
21


 
1
<?xml version='1.0' encoding='UTF-8'?>
2

          
3
<DepartmentType>
4

          
5
  <Employee>
6

          
7
    <Name>Jolly</Name>
8

          
9
    <Designation>Science Teacher</Designation>
10

          
11
  </Employee>
12

          
13
  <Employee>
14

          
15
    <Name>Chrissy</Name>
16

          
17
    <Designation>Maths Teacher</Designation>
18

          
19
  </Employee>
20

          
21
</DepartmentType>



Scenario #2: There might be some requirement where we want to remove some unused data from the payload and use the filter function.

Input:

Java
 




xxxxxxxxxx
1
36


 
1
[
2

          
3
{
4

          
5
    "EmpName": "John",
6

          
7
    "Age": "32",
8

          
9
    "ServiceDetails":{
10

          
11
        "Designation": "Clerk",
12

          
13
        "DOJ": "Sept 20 1998"
14

          
15
    }
16

          
17
},
18

          
19
{
20

          
21
    "EmpName": "Kelvin",
22

          
23
    "Age": "33",
24

          
25
    "ServiceDetails":{
26

          
27
        "Designation": "Accountant",
28

          
29
        "DOJ": "Sept 20 2015"
30

          
31
    }
32

          
33
}
34

          
35
]
36

          



DataWeave Code:

%dw 2.0

Output application/json

Payload map ($ - 'ServiceDetails') filter ($.'Age'>32)

Output:

Java
 




xxxxxxxxxx
1
11


 
1
[
2

          
3
  {
4

          
5
    "EmpName": "Kelvin",
6

          
7
    "Age": "33"
8

          
9
  }
10

          
11
]



I hope these examples will help you. I will be happy to answer any questions. 

Filter (software)

Opinions expressed by DZone contributors are their own.

Related

  • Querying Without a Query Language
  • Tableau Dashboard Development Best Practices
  • An Introduction to Bloom Filters
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook