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

  • Give Your AI Assistant Long-Term Memory With perag
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives

Trending

  • Grok AI API Tutorial: Chat, Image, Video, Tool Calling, and Web Search
  • I Reverse-Engineered 50 API Breaches. The Same Five Mistakes Keep Appearing.
  • Your AI Coding Agent Can't Steal What It Never Had: The Docker Sandbox Isolation Story
  • Give Your AI Assistant Long-Term Memory With perag
  1. DZone
  2. Coding
  3. Languages
  4. Deep Dive Into DataWeave Core Arrays Module: Part 1

Deep Dive Into DataWeave Core Arrays Module: Part 1

In this article, we will be using a JSON payload for understanding various functions with DW Core Arrays.

By 
Jitendra Bafna user avatar
Jitendra Bafna
·
Jan. 15, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
6.9K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

DataWeave Core Arrays module provides the helper function which can be used on the array of items. There are many functions provided by the DW Core Array module like sumBy, countBy, some, every, etc.

In this article, we will be using the below JSON payload for understanding various functions with DW Core Arrays.

JSON
 




x
37


 
1
 [
2
      {
3
        "id": "1",
4
        "firstName": "Tom",
5
        "lastName": "Cruise",
6
        "age":25,
7
        "salary":20000
8
      },
9
      {
10
        "id": "2",
11
        "firstName": "Maria",
12
        "lastName": "Sharapova",
13
        "age":28,
14
        "salary":10000
15
      },
16
      {
17
        "id": "3",
18
        "firstName": "James",
19
        "lastName": "Bond",
20
        "age":32,
21
        "salary":30000
22
      },
23
      {
24
        "id": "4",
25
        "firstName": "Kevin",
26
        "lastName": "Peter",
27
        "age":35,
28
        "salary":40000
29
      },
30
      {
31
        "id": "5",
32
        "firstName": "Jackie",
33
        "lastName": "Chen",
34
        "age":40,
35
        "salary":50000
36
      }
37
    ]



Example 1

We will use some function to check whether list having 

  • At least one employee salary less than 40000 
  • At least one employee salary greater than 50000

some returns true if at least one element in the array matches the specified condition. 

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
import * from dw::core::Arrays
3
output application/json
4
---
5
{
6
    employeeSalaryLessThan40000: payload..salary some ($ < 40000), //syntax 1
7
    employeeSalaryGreaterThan50000: some(payload..salary, (value)->(value > 50000))  //syntax 2
8
}



Output

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "employeeSalaryLessThan40000": true,
3
  "employeeSalaryGreaterThan50000": false
4
}



Example 2

We will use every function to check whether list having 

  • At least all employees salary less than or equal to 50000.
  • At least all employees salary greater than 50000.

every returns true if every element in the array matches the condition.

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
import * from dw::core::Arrays
3
output application/json
4
---
5
{
6
    employeeSalaryLessThanorEqual50000: payload..salary every ($ <= 50000), //syntax 1
7
    employeeSalaryGreaterThan50000: every(payload..salary, (value)->(value > 50000))  //syntax 2
8
}



Output

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "employeeSalaryLessThanorEqual50000": true,
3
  "employeeSalaryGreaterThan50000": false
4
}



Example 3

We will use countBy function to check whether list having

  • Count employee whose salary less than or equal to 30000.
  • Count employee whose salary greater than 40000.

countBy counts the elements in an array that match the results of a function. 

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
import * from dw::core::Arrays
3
output application/json
4
---
5
{
6
    countEmployeeSalaryLessThanorEqual30000: payload..salary countBy ($ <= 30000), //syntax 1
7
    countEmployeeSalaryGreaterThan40000: countBy(payload..salary, (value)->(value > 40000))  //syntax 2
8
}



Output

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "countEmployeeSalaryLessThanorEqual30000": 3,
3
  "countEmployeeSalaryGreaterThan40000": 1
4
}



Example 4

We will use sumBy function to check whether list having

  • Sum employee count whose salary less than or equal to 30000.
  • Sum employee count whose salary greater than 40000.

sumBy returns the sum of the values of the elements in an array. 

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
import * from dw::core::Arrays
3
output application/json
4
---
5
{
6
    sumEmployeeSalaryLessThanorEqual30000: payload..salary sumBy (if($ <= 30000) $ else 0), //syntax 1
7
    sumEmployeeSalaryGreaterThan40000: sumBy(payload..salary, (value)->(if(value > 40000) value else 0)) //syntax 2
8
}



Output

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "sumEmployeeSalaryLessThanorEqual30000": 60000,
3
  "sumEmployeeSalaryGreaterThan40000": 50000
4
}




Now, you know DataWeave Core Arrays Module helper functions.

JSON

Opinions expressed by DZone contributors are their own.

Related

  • Give Your AI Assistant Long-Term Memory With perag
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives

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