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

  • DataWeave Interview Question: Concatenate Elements of an Array
  • Merge Multiple PDFs in MuleSoft
  • DataWeave Interview Question: Find Unique Names From the Input
  • Dataweave Interview Question Using Map and Reduce

Trending

  • From Data Movement to Local Intelligence: The Shift from Centralized to Federated AI
  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array

DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array

This article will help you practice your DataWeave skills in MuleSoft. We're going to compare IDs from two arrays of objects and create a new array.

By 
Gaurav Dubey user avatar
Gaurav Dubey
·
Feb. 13, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
15.8K Views

Join the DZone community and get the full member experience.

Join For Free

This article will help you practice your DataWeave skills in MuleSoft. Let's get started.

Input:

1)

Java
 




x
35


 
1
[
2
        {
3
            name : "Sachin",
4
            id:1
5
        },
6
        {
7
            name : "Mahendra",
8
            id:2
9
        },
10
        {
11
            name: "Gaurav",
12
            id: 3
13
        },
14
        {
15
            name: "Sidharth",
16
            id: 4
17
        }
18
]



2)

Java
 




xxxxxxxxxx
1
35


 
1
[
2
        {
3
            id:1,
4
            Age: 28,
5
            Gender: 'M'
6
        },
7
        {
8
            id:4,
9
            Age: 29,
10
            Gender: 'M'
11
        },
12
        {
13
            id: 3,
14
            Age: 25,
15
            Gender: 'F'
16
        }
17
]



Output:

Java
 




xxxxxxxxxx
1
39


 
1
[
2
  {
3
    "id": 1,
4
    "Age": 28,
5
    "Gender": "M",
6
    "name": "Sachin"
7
  },
8
  {
9
    "id": 3,
10
    "Age": 25,
11
    "Gender": "F",
12
    "name": "Gaurav"
13
  },
14
  {
15
    "id": 4,
16
    "Age": 29,
17
    "Gender": "M",
18
    "name": "Sidharth"
19
  }
20
]


Let's talk about the solution now. We are going to achieve this in multiple steps as follows:

Step 1

In step 1, we will store the second input into the input2 variable.

Step 1 Code:

Java
 




xxxxxxxxxx
1
39


 
1
%dw 2.0
2
output application/json
3
var input2=[
4
       { 
5
           "id":1,
6
           "Age": 28,
7
           "Gender": 'M'
8
       },
9
       {
10
           "id":4,
11
           "Age": 29,
12
           "Gender": 'M'
13
       },
14
       {
15
           "id": 3,
16
           "Age": 25,
17
           "Gender": 'F'
18
       }
19
]



Step 2

In step 2, we will first apply a map to the payload.

After that, we will apply a map on the input2 variable inside our previous map.

After that, we will check to create the desired output if the ID of the payload matches the ID of input2.

If the condition is false then we will use null.

Step 2 Code:

Java
 




xxxxxxxxxx
1
51


 
1
%dw 2.0
2
output application/json
3
var input2=[
4
        {        
5
            "id":1,
6
            "Age": 28,
7
            "Gender": 'M'
8
        },
9
        {
10
            "id":4,
11
            "Age": 29,
12
            "Gender": 'M'
13
        },
14
        {
15
            "id": 3,
16
            "Age": 25,
17
            "Gender": 'F'
18
        }
19
]
20
 
          
21
---
22
 
          
23
payload map ((item, index) -> input2 map ((item1, index1) -> if(item.id == item1.id){
24
    "id": item1.id,
25
    "Age": item1.Age,
26
    "Gender":item1.Gender,
27
    "name": item.name 
28
} else null))



Step 2 Output:

Java
 




xxxxxxxxxx
1
73


 
1
[
2
  [
3
    {
4
      "id": 1,
5
      "Age": 28,
6
      "Gender": "M",
7
      "name": "Sachin"
8
    },
9
    null,
10
    null
11
  ],
12
  [
13
    null,
14
    null,
15
    null
16
  ],
17
  [
18
    null,
19
    null,
20
    {
21
      "id": 3,
22
      "Age": 25,
23
      "Gender": "F",
24
      "name": "Gaurav"
25
    }
26
  ],
27
  [
28
    null,
29
    {
30
      "id": 4,
31
      "Age": 29,
32
      "Gender": "M",
33
      "name": "Sidharth"
34
    },
35
    null
36
  ]
37
]


Step 3

In the final step, we are applying flatten to the whole output and using a filter to show records that are not null.

Step 3 Code:

Java
 




xxxxxxxxxx
1
53


 
1
%dw 2.0
2
output application/json
3
var input2=[
4
        {
5
            "id":1,
6
            "Age": 28,
7
            "Gender": 'M'
8
        },
9
        {
10
            "id":4,
11
            "Age": 29,
12
            "Gender": 'M'
13
        },
14
        {
15
            "id": 3,
16
            "Age": 25,
17
            "Gender": 'F'
18
        }
19
]
20
 
          
21
---
22
 
          
23
flatten(payload map ((item, index) -> input2 map ((item1, index1) -> if(item.id == item1.id){
24
    "id": item1.id,
25
    "Age": item1.Age,
26
    "Gender":item1.Gender,
27
    "name": item.name 
28
} else null))) filter $ != null



Hope this helps improve your DataWeave skills. I am not an expert on DataWeave. I am just sharing what I can solve. Comments are welcome. Thanks.

Java (programming language) Data structure Interview (journalism) code style Payload (computing) Filter (software) Record (computer science) MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • DataWeave Interview Question: Concatenate Elements of an Array
  • Merge Multiple PDFs in MuleSoft
  • DataWeave Interview Question: Find Unique Names From the Input
  • Dataweave Interview Question Using Map and Reduce

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