Array 1:
[
{
"name": "Abc",
"address":"Mumbai",
"phone":"91-1234567891",
"id":123
},
{
"name": "Pqr",
"address":"Pune",
"phone":"91-1234985438",
"id":456
},
{
"name": "Abc",
"address":"Delhi",
"phone":"91-1234567891",
"id":123
}
]
Array 2:
[
{
"sid":123,
"postalCode": "67890",
"country": "USA",
"sname": "tyu"
},
{
"sid":864,
"postalCode": "54983",
"country": "UK",
"sname": "Pqr"
},
{
"sid":123,
"postalCode": "27932",
"country": "EU",
"sname": "Uvg"
}
]
Above we have to consider both ID and name, if either of the fields match as leftjoin then we have to consider them.
Expected Output:
[
{
"name": "Abc",
"address": "Mumbai",
"phone": "91-1234567891",
"id": 123,
"sid": 123,
"sname": "tyu",
"postalCode": "27932",
"country": "EU"
},
{
"name": "Pqr",
"address": "Pune",
"phone": "91-1234985438",
"id": 456,
"sid": 864,
"sname": "Pqr",
"postalCode": "54983",
"country": "UK"
},
{
"name": "Abc",
"address": "Delhi",
"phone": "91-1234567891",
"id": 123,
"sid": 123,
"sname": "Uvg",
"postalCode": "27932",
"country": "EU"
}
]
Dataweave:
%dw 2.0
output application/json
import * from dw::core::Arrays
var Array2reversed=Array2[-1 to 0] distinctBy ((item, index) -> item.sid)
var combine=leftJoin(Array1, Array2reversed, (Array1) -> Array1.name, (Array2) -> Array2.sname) map $
---
(leftJoin(combine, Array2reversed, (Array1) -> Array1.id, (Array2) -> Array2.sid))
map {
"name": $.l.name,
"address": $.l.address,
"phone": $.l.phone,
"id": $.l.id,
"sid": $.r.sid,
"postalCode": $.r.postalCode,
"country": $.r.country
}.
We have to consider name and sname as another condition in left join. I tried to give in function but I m getting error and not able to join with multiple conditions in one join only.