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

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • NFT Wallets Unleashed: A Data Structures and Application Design Journey
  • JSON Minify Full Guideline: Easy For You
  • Adding Two Hours in DataWeave: Mule 4

Trending

  • How Has AI Impacted Code Obfuscation?
  • Idempotent Consumers: Preventing Stale Retries With Version-Fenced Writes
  • Building Fault-Tolerant MCP Servers With Temporal for Durable AI Agent Execution
  • Part 5: Automated Agent Evaluation and Regression Testing for MCP Tool Services

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. Data Engineering
  3. Data
  4. Deep Dive Into DataWeave Core Arrays Module: Part II

Deep Dive Into DataWeave Core Arrays Module: Part II

This article explores various types of

By 
Jitendra Bafna
·
Jan. 16, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.7K 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

In the last article, we saw various DW Core Arrays helper functions like some, every, sumBy, and countBy. Here we are going to see various types of joins provided by the DW Core Arrays module.

join combines8 two arrays of objects by a given ID criteria. There are three types of joins provided by the MuleSoft DW Core Arrays Module.

  • join
  • leftJoin
  • outerJoin

We will be using below employee and department payload to understand the joins.

Employee

JSON
 




x


 
1
[{
2
    "employeeId":1,
3
    "deptId":1,
4
    "employeeName": "James Thomas"
5
    },
6
    {
7
    "employeeId":2,
8
    "deptId":3,
9
    "employeeName": "James Peter"
10
    },
11
    {
12
    "employeeId":3,
13
    "deptId":3,
14
    "employeeName": "Jackie Chen"
15
    },
16
    {
17
    "employeeId":4,
18
    "deptId":4,
19
    "employeeName": "Jackie Chen"
20
}]



Department

JSON
 




x





1
[{
2
    "deptId":"1",
3
    "deptName":"Science"
4
    },
5
    {
6
    "deptId":"2",
7
    "deptName":"Information Technology"
8
    },
9
    {
10
    "deptId":"3",
11
    "deptName":"Physics"
12
}]



Each Employee is associated with one department.

join

join returns an array of all the left items, merged by ID with any right items that exist. 

It takes four parameters:

  • left — The left side array of the object.
  • right — The right side array of the object.
  • leftCriteria — The criteria is used to extract ID of the left collection.
  • rightCriteria — The criteria is used to extract ID of the right collection.
JSON
 




x


 
1
%dw 2.0
2
output application/json
3
import * from dw::core::Arrays
4
var employee=
5
[{
6
    "employeeId":1,
7
    "deptId":1,
8
    "employeeName": "James Thomas"
9
    },
10
    {
11
    "employeeId":2,
12
    "deptId":3,
13
    "employeeName": "James Peter"
14
    },
15
    {
16
    "employeeId":3,
17
    "deptId":3,
18
    "employeeName": "Jackie Chen"
19
    },
20
    {
21
    "employeeId":4,
22
    "deptId":4,
23
    "employeeName": "Jackie Chen"
24
    }]
25

          
26
    var department=
27
    [{
28
    "deptId":"1",
29
    "deptName":"Science"
30
    },
31
    {
32
    "deptId":"2",
33
    "deptName":"Information Technology"
34
    },
35
    {
36
    "deptId":"3",
37
    "deptName":"Physics"
38
}]
39
---
40
join(employee,department,(emp)->(emp.deptId),(dept)->(dept.deptId))



Output

JSON
 




x
8
37


 
1
[
2
  {
3
    "l": {
4
      "employeeId": 1,
5
      "deptId": 1,
6
      "employeeName": "James Thomas"
7
    },
8
    "r": {
9
      "deptId": "1",
10
      "deptName": "Science"
11
    }
12
  },
13
  {
14
    "l": {
15
      "employeeId": 2,
16
      "deptId": 3,
17
      "employeeName": "James Peter"
18
    },
19
    "r": {
20
      "deptId": "3",
21
      "deptName": "Physics"
22
    }
23
  },
24
  {
25
    "l": {
26
      "employeeId": 3,
27
      "deptId": 3,
28
      "employeeName": "Jackie Chen"
29
    },
30
    "r": {
31
      "deptId": "3",
32
      "deptName": "Physics"
33
    }
34
  }
35
]
36
   
37
  



leftJoin

leftJoin returns an array of all the left items, merged by ID with any right items that meet the joining criteria.

It takes four parameters:

  • left — The left side array of the object.
  • right — The right side array of the object.
  • leftCriteria — The criteria is used to extract ID of left collection.
  • rightCriteria — The criteria is used to extract ID of right collection.
JSON
 




x


 
1
%dw 2.0
2
output application/json
3
import * from dw::core::Arrays
4
var employee=
5
[{
6
    "employeeId":1,
7
    "deptId":1,
8
    "employeeName": "James Thomas"
9
    },
10
    {
11
    "employeeId":2,
12
    "deptId":3,
13
    "employeeName": "James Peter"
14
    },                          
15
    {
16
    "employeeId":3,
17
    "deptId":3,
18
    "employeeName": "Jackie Chen"
19
    },
20
    {
21
    "employeeId":4,
22
    "deptId":4,
23
    "employeeName": "Jackie Chen"
24
}]
25

          
26
var department=
27
[{
28
    "deptId":"1",
29
    "deptName":"Science"
30
    },
31
    {
32
    "deptId":"2",
33
    "deptName":"Information Technology"
34
    },
35
    {
36
    "deptId":"3",
37
    "deptName":"Physics"
38
}]
39
---
40
leftJoin(employee,department,(emp)->(emp.deptId),(dept)->(dept.deptId))-



Output

JSON
 




xxxxxxxxxx
1
42


 
1
[
2
  {
3
    "l": {
4
      "employeeId": 1,
5
      "deptId": 1,
6
      "employeeName": "James Thomas"
7
    },
8
    "r": {
9
      "deptId": "1",
10
      "deptName": "Science"
11
    }
12
  },
13
  {
14
    "l": {
15
      "employeeId": 2,
16
      "deptId": 3,
17
      "employeeName": "James Peter"
18
    },
19
    "r": {
20
      "deptId": "3",
21
      "deptName": "Physics"
22
    }
23
  },
24
  {
25
    "l": {
26
      "employeeId": 3,
27
      "deptId": 3,
28
      "employeeName": "Jackie Chen"
29
    },
30
    "r": {
31
      "deptId": "3",
32
      "deptName": "Physics"
33
    }
34
  },
35
  {
36
    "l": {
37
      "employeeId": 4,
38
      "deptId": 4,
39
      "employeeName": "Jackie Chen"
40
    }
41
  }
42
]



outerJoin

outerJoin returns an array with all the left items, merged by ID with the right items in cases where any exist, and it returns right items that are not present in the left.

It takes four parameters.

  • left — The left side array of the object.
  • right — The right side array of the object.
  • leftCriteria — The criteria is used to extract ID of left collection.
  • rightCriteria — The criteria is used to extract ID of right collection.
JSON
 




x





1
%dw 2.0
2
output application/json
3
import * from dw::core::Arrays
4
var employee=
5
[{
6
    "employeeId":1,
7
    "deptId":1,
8
    "employeeName": "James Thomas"
9
    },
10
    {
11
    "employeeId":2,
12
    "deptId":3,
13
    "employeeName": "James Peter"
14
    },
15
    {
16
    "employeeId":3,
17
    "deptId":3,
18
    "employeeName": "Jackie Chen"
19
    },
20
    {
21
    "employeeId":4,
22
    "deptId":4,
23
    "employeeName": "Jackie Chen"
24
}]
25

          
26
var department=
27
[{
28
    "deptId":"1",
29
    "deptName":"Science"
30
    },
31
    {
32
    "deptId":"2",
33
    "deptName":"Information Technology"
34
    },
35
    {
36
    "deptId":"3",
37
    "deptName":"Physics"
38
}]
39
---
40
outerJoin(employee,department,(emp)->(emp.deptId),(dept)->(dept.deptId))



Output

JSON
 




xxxxxxxxxx
1
48


 
1
[
2
  {
3
    "l": {
4
      "employeeId": 1,
5
      "deptId": 1,
6
      "employeeName": "James Thomas"
7
    },
8
    "r": {
9
      "deptId": "1",
10
      "deptName": "Science"
11
    }
12
  },
13
  {
14
    "l": {
15
      "employeeId": 2,
16
      "deptId": 3,
17
      "employeeName": "James Peter"
18
    },
19
    "r": {
20
      "deptId": "3",
21
      "deptName": "Physics"
22
    }
23
  },
24
  {
25
    "l": {
26
      "employeeId": 3,
27
      "deptId": 3,
28
      "employeeName": "Jackie Chen"
29
    },
30
    "r": {
31
      "deptId": "3",
32
      "deptName": "Physics"
33
    }
34
  },
35
  {
36
    "l": {
37
      "employeeId": 4,
38
      "deptId": 4,
39
      "employeeName": "Jackie Chen"
40
    }
41
  },
42
  {
43
    "r": {
44
      "deptId": "2",
45
      "deptName": "Information Technology"
46
    }
47
  }
48
]



There is no direct rightJoin but you can still achieve using leftJoin. In the leftJoin, you can bring a department in the left and an employee in the right as shown below:

JSON
 




x





1
%dw 2.0
2
output application/json
3
import * from dw::core::Arrays
4
var employee=
5
[{
6
    "employeeId":1,
7
    "deptId":1,
8
    "employeeName": "James Thomas"
9
    },
10
    {
11
    "employeeId":2,
12
    "deptId":3,
13
    "employeeName": "James Peter"
14
    },
15
    {
16
    "employeeId":3,
17
    "deptId":3,
18
    "employeeName": "Jackie Chen"
19
    },
20
    {
21
    "employeeId":4,
22
    "deptId":4,
23
    "employeeName": "Jackie Chen"
24
}]
25
26
var department=
27
[{
28
    "deptId":"1",
29
    "deptName":"Science"
30
    },
31
    {
32
    "deptId":"2",
33
    "deptName":"Information Technology"
34
    },
35
    {
36
    "deptId":"3",
37
    "deptName":"Physics"
38
}]
39
---
40
leftJoin(department,employee,(dept)->(dept.deptId),(emp)->(emp.deptId))



Output

JSON
 




xxxxxxxxxx
1
41


 
1
[
2
  {
3
    "l": {
4
      "deptId": "1",
5
      "deptName": "Science"
6
    },
7
    "r": {
8
      "employeeId": 1,
9
      "deptId": 1,
10
      "employeeName": "James Thomas"
11
    }
12
  },
13
  {
14
    "l": {
15
      "deptId": "2",
16
      "deptName": "Information Technology"
17
    }
18
  },
19
  {
20
    "l": {
21
      "deptId": "3",
22
      "deptName": "Physics"
23
    },
24
    "r": {
25
      "employeeId": 2,
26
      "deptId": 3,
27
      "employeeName": "James Peter"
28
    }
29
  },
30
  {
31
    "l": {
32
      "deptId": "3",
33
      "deptName": "Physics"
34
    },
35
    "r": {
36
      "employeeId": 3,
37
      "deptId": 3,
38
      "employeeName": "Jackie Chen"
39
    }
40
  }
41
]


Now, you know how to implement joins with MuleSoft DataWeave.

JSON Data structure

Opinions expressed by DZone contributors are their own.

Related

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • NFT Wallets Unleashed: A Data Structures and Application Design Journey
  • JSON Minify Full Guideline: Easy For You
  • Adding Two Hours in DataWeave: Mule 4

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