Dataweave Exercise – DZone Integration

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, dropWhilewhich 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

[
    {
        "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 Inputand 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.

.

Leave a Comment