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

  • 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

  • AI-Assisted Refinement Preparation
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • A Software Factory on Kubernetes: Gemini Builds, jev Decides (Genkit + senro in Go)
  • Building Multi-Rail Payment Failover to Stop Latency Spikes and Double Debits

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. 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
·
Feb. 13, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.4K 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

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

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