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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Part 5: Automated Agent Evaluation and Regression Testing for MCP Tool Services
  • Migrating From the OpenAI API to vLLM: Configure It Right
  • Constrained Decoding Models Explained: Encoders, Decoders and When to Use Them
  • Building a Reddit Scraper in Go: JSON Listings, Pagination, Rate Limits, and Where It Breaks

Trending

  • Why Successful LLM Demos Fail in Production
  • Avoiding AI Debt as a Team
  • Google Cloud Pub/Sub vs. AWS SNS and SQS: One Java API for Both With MultiCloudJ
  • Prompt Caching for Multi-Turn Coding Agents: Why Append-Only Transcripts Cut Your Token Bill

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jan. 15, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Part 5: Automated Agent Evaluation and Regression Testing for MCP Tool Services
  • Migrating From the OpenAI API to vLLM: Configure It Right
  • Constrained Decoding Models Explained: Encoders, Decoders and When to Use Them
  • Building a Reddit Scraper in Go: JSON Listings, Pagination, Rate Limits, and Where It Breaks

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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