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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Streamlining Event Data in Event-Driven Ansible
  • Dynamic Web Forms In React For Enterprise Platforms
  • Unlocking Oracle 23 AI's JSON Relational Duality
  • Loading XML into MongoDB

Trending

  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • The Role of AI in Identity and Access Management for Organizations
  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
DZone Core CORE ·
Jan. 15, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
6.7K 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

  • Streamlining Event Data in Event-Driven Ansible
  • Dynamic Web Forms In React For Enterprise Platforms
  • Unlocking Oracle 23 AI's JSON Relational Duality
  • Loading XML into MongoDB

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!