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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

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

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Dataweave Exercise: Filter Like Functions in Arrays Module - Part 1
  • 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

Trending

  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Solid Testing Strategies for Salesforce Releases
  • Is Agile Right for Every Project? When To Use It and When To Avoid It

DataWeave Exercise: Usage of Filter Function

In this DataWeave exercise, learn more about how to filter an array using DataWeave's core module function filter.

By 
Venkata Sai Krishna Thotapalli user avatar
Venkata Sai Krishna Thotapalli
·
Jan. 27, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
7.1K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog, I'll demonstrate how to use DataWeave filter functions in various ways.

Input:

All the examples below use the same input.

JSON
 
[
    {
        "id": "123",
        "name": "John",
        "salary": 10000,
        "age": 61
    },
    {
        "id": "456",
        "name": "Charles",
        "salary": 20000,
        "age": 40
    },
    {
        "id": "789",
        "name": "Paul",
        "salary": 30000,
        "age": 30
    },
    {
        "id": "101",
        "name": "George",
        "salary": 40000,
        "age": 62
    }
]

Example 1

Desired Output:

Filter the given Input using the condition age is less than 60 to get the output shown below.

JSON
 
[
  {
    "id": "456",
    "name": "Charles",
    "salary": 20000,
    "age": 40
  },
  {
    "id": "789",
    "name": "Paul",
    "salary": 30000,
    "age": 30
  }
]

The above output can be obtained in a number of ways.

Using Filter:

Java
 
%dw 2.0
output application/json
---
payload filter $.age < 60

The input array is filtered in the above code using the filter function of the core module, where '$' refers to each object in the array, '$.age' refers to the input object's "age" field, and '$.age < 60' is the filter condition.

Using Filter Expression (Selector):

Java
 
%dw 2.0
output application/json
---
payload [?($.age < 60)]

Instead of writing a filter function, the array is filtered using a filter expression, which requires square brackets, a question mark, and a filter condition inside the parenthesis.

Using Filter With Map Functions:

Java
 
%dw 2.0
output application/json
---
payload filter $.age < 60 map {
     "id" : $.id,
    "name" : $.name,
    "Salary" : $.salary,
    "age" : $.age
}

The above code applies the filter to the input payload and then uses the map function. The same can be written in another way as below.

Java
 
%dw 2.0
output application/json
---
payload  map {
     "id" : $.id,
    "name" : $.name,
    "Salary" : $.salary,
    "age" : $.age
} filter $.age < 60

The map function is applied to the input payload, and then the filter is applied to the result of the map.

Using Filter Expression With Map Function:

Java
 
%dw 2.0
output application/json
---
payload  [?($.age < 60)] map {
     "id" : $.id,
    "name" : $.name,
    "Salary" : $.salary,
    "age" : $.age
} 

It's similar to using the filter function, but filter expression is used instead.

Example 2

Desired Output:

Filter the given Input using the condition age is less than 60 to get the output shown below.

JSON
 
[
  {
    "name": "Charles",
    "Salary": 20000,
    "id": "456"
  },
  {
    "name": "Paul",
    "Salary": 30000,
    "id": "789"
  }
]

Using Filter With Map:

Java
 
%dw 2.0
output application/json
---
payload filter $.age < 60  map {
     "id" : $.id,
    "name" : $.name,
    "Salary" : $.salary,
 }

After filtering the input array (payload), the map was used. It can't be done the other way around, as shown below. This won't work because the filter is applied to the map's output, which doesn't have an age field, hence, why it won't work in this case.

Java
 
%dw 2.0
output application/json
---
payload   map {
     "id" : $.id,
    "name" : $.name,
    "Salary" : $.salary,
 } filter $.age < 60

Using Filter With Map and “-” Operator:

Java
 
%dw 2.0
output application/json
---
payload filter $.age < 60  map {
    ($)
} - "age"

Here, the filter is applied on a given input array with the map function. Since the "age" field is not required in the output, "-" is used to remove it.

Using Filter Expression With Map Function:

Java
 
%dw 2.0
output application/json
---
payload [?($.age < 60)]  map {
     "id" : $.id,
    "name" : $.name,
    "Salary" : $.salary,
 }  

Instead of the filter function used, the expression is used.

Filter (software) Exercise (mathematics)

Opinions expressed by DZone contributors are their own.

Related

  • Dataweave Exercise: Filter Like Functions in Arrays Module - Part 1
  • 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

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!