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

  • An Introduction to Bloom Filters
  • Reversing an Array: An Exploration of Array Manipulation
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Sliding Window

Trending

  • Architecting Petabyte-Scale Hyperspectral Pipelines on AWS
  • Why Your QA Engineer Should Be the Most Stubborn Person on the Team
  • Throughput vs Goodput: The Performance Metric You Are Probably Ignoring in LLM Testing
  • RAG Done Right: When to Use SQL, Search, and Vector Retrieval and How To Combine Them
  1. DZone
  2. Data Engineering
  3. Data
  4. Dataweave Exercise: Filter Like Functions in Arrays Module - Part 1

Dataweave Exercise: Filter Like Functions in Arrays Module - Part 1

Today, I am going to show you how to use a few functions in the arrays module that are similar to but not equivalent to the filter function.

By 
Venkata Sai Krishna Thotapalli user avatar
Venkata Sai Krishna Thotapalli
·
Feb. 05, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
8.8K Views

Join the DZone community and get the full member experience.

Join For Free

Functions are divided into modules in dataweave, just as methods are divided into packages in Java. Today I'll show you how to use a few functions in the arrays module that are similar to but not equivalent to the filter function.

Arrays module functions that we are going to discuss are drop, dropWhile, which are introduced in the dataweave 2.2.0 version. The reason for comparing these functions to filter is that, just like filter, they also operate on an array to produce the desired result based on some criteria.

To use functions from the Arrays module in dataweave code we should import them at header section as below:

 import * from dw::core::Arrays 

Similarly, if you want to use functions from other modules, you can do so:

 import * from dw::core:://module name// 

Just at the end module name changes.

Let's get started discussing functions; I'll be using the Input provided below to explain all the functions.

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
    }
]


drop

This function is used to eliminate elements from an array. It has two parameters, the first one is the array, and the other one is the n (n is no. of elements to be dropped from the array). If n is greater than the array size, an empty array is returned, and if n is less than or equal to zero, the original array is returned.

The dataweave script below eliminates the first three elements from the array and returns it as an array with the remaining elements.

Dataweave Script

 
%dw 2.0
import * from dw::core::Arrays
output application/json
---
drop(payload,3)


Output

 
[
  {
    "id": "101",
    "name": "George",
    "salary": 40000,
    "age": 62
  }
]


dropWhile

This function is used to drop elements from an array based on criteria. It has two parameters: array and condition, which is used to match the array's elements. When the condition is met, it will drop the elements from the array, and when it reaches an element that does not satisfy the condition, it will stop the element selection process.

Now we'll look at some examples to see if we can learn it out.

Example 1

Dataweave Script  

In the script below dropwhile condition is $.age > 60, which means it should drop the elements with an age greater than 60.

 
%dw 2.0
import * from dw::core::Arrays
output application/json
---
payload dropWhile $.age > 60


Output

If you look at the output, you'll notice that the array contains objects with ages greater than 60. Even though our condition in the Dataweave Script says it should drop objects over the age of 60, it doesn't because it dropped the first object from the Input, and the second object in the array didn't match the criteria, so the object selection process was halted.

 
[
  {
    "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 2

In this example, we will take different input as below:

Input

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

  

Dataweave Script

 
%dw 2.0
import * from dw::core::Arrays
output application/json
---
payload dropWhile $.age > 60

  

Output

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


There are no objects with an age greater than 60 in the above output;  If you notice, Example 2 input has the first two objects with ages greater than 60. As a result, drop While the function begins the selection process with the criteria provided, if the criteria match, the elements are dropped; if the criteria do not match, the selection process is terminated.

Thank you for taking the time to read this article. If you still have any questions, read the dataweave array module.

Data structure Element Filter (software) Exercise (mathematics)

Opinions expressed by DZone contributors are their own.

Related

  • An Introduction to Bloom Filters
  • Reversing an Array: An Exploration of Array Manipulation
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Sliding Window

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