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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • An Introduction to Bloom Filters
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases
  • Filtering Java Collections via Annotation-Driven Introspection
  • Exploring Google's Open Images V7

Trending

  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • GitHub Copilot's New AI Coding Agent Saves Developers Time – And Requires Their Oversight
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs

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 (7)
Comment
Save
Tweet
Share
25.4K 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

  • An Introduction to Bloom Filters
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases
  • Filtering Java Collections via Annotation-Driven Introspection
  • Exploring Google's Open Images V7

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!