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

  • Dataweave Exercise: Filter Like Functions in Arrays Module - Part 1
  • Querying Without a Query Language
  • Tableau Dashboard Development Best Practices
  • An Introduction to Bloom Filters

Trending

  • Every Cache Miss Is a Tiny Tax on Your Performance
  • The Middleware Gap in AI Agent Frameworks
  • Evolving Spring Boot APIs to an Event-Driven Mesh
  • Spring Boot Done Right: Lessons From a 400-Module Codebase

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.4K 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
  • Querying Without a Query Language
  • Tableau Dashboard Development Best Practices
  • An Introduction to Bloom Filters

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