Satyam Pisal
Satyam Pisal

Reputation: 126

Dataweave: Merge 2 arrays based upon condition

I have 2 arrays stored in a variable as

vars.array1

[
    {
        "id": 123,
        "name": "Sam",
        "class": "first"
    },
    {
        "id": 124,
        "name": "John",
        "class": "first"
    },
    {
        "id": 125,
        "name": "Max",
        "class": "second"
    }
]

vars.array2

[
    {
        "studentId": 123,
        "course": "science",
        "otherActivities": "sports"
    },
    {
        "studentId": 125,
        "course": "arts",
        "otherActivities": "studentCouncil"
    },
    {
        "studentId": 126,
        "course": "literature",
        "otherActivities": "drama"
    }
]

Expected Output:

[
    {
        "id": 123,
        "name": "Sam",
        "class": "first",
        "course": "science",
        "otherActivities": "sports"
    },
    {
        "id": 125,
        "name": "Max",
        "class": "second",
        "course": "arts",
        "otherActivities": "studentCouncil"
    }
]

The original arrays contains around 5000.

Right now, i am using this as solution,

//this code is inside 'for each' with collection as vars.array1
//vars.array3 is inilialised as [] before 'for each'
%dw 2.0
output application/json
---
if(sizeOf(vars.array2 filter $["studentId"] == payload.id) > 0)
(vars.array3 << (payload ++ (vars.array2 filter $["studentId"] == payload.id)[0]))
else vars.array3

This works fine with around 500 records but takes time 5k records. Wondering if there is any other way to reduce the complexity which would give faster response.

Upvotes: 0

Views: 2020

Answers (2)

Salim Khan
Salim Khan

Reputation: 4303

Have you tried using join ?

Script

%dw 2.0
output application/json
import * from dw::core::Arrays
var array1=[
    {
        "id": 123,
        "name": "Sam",
        "class": "first"
    },
    {
        "id": 124,
        "name": "John",
        "class": "first"
    },
    {
        "id": 125,
        "name": "Max",
        "class": "second"
    }
]
var array2=[
    {
        "studentId": 123,
        "course": "science",
        "otherActivities": "sports"
    },
    {
        "studentId": 125,
        "course": "arts",
        "otherActivities": "studentCouncil"
    },
    {
        "studentId": 126,
        "course": "literature",
        "otherActivities": "drama"
    }
]
---
join(array1, array2, (a1) -> a1.id, (a2) -> a2.studentId) map {
     ($.l ++ $.r - "studentId" )
}

Upvotes: 5

Dhwaj Kothari
Dhwaj Kothari

Reputation: 41

If your arrays are sorted using id then you can use binary search to chop off your complexity of search.

Upvotes: 0

Related Questions