Reputation: 63
Need to compare two arrays efficiently and create a third array with values that are only in the second array using Dataweave transformation in mule. I wanted to use the negation of contains the keyword in mule. But it was giving errors. Hope that I can use a filter and Contains to filter out the values.
arr1 =[
{
"leadId": 127,
"playerId": 334353
},
{
"leadId": 128,
"playerId": 334354
},
{
"leadId": 123,
"playerId": 43456
}
{
"leadId": 122,
"playerId": 43458
}
arr2 =[
{
"leadId": 127,
"name": "James"
},
{
"leadId": 129,
"name": "Joseph"
},
{
"leadId": 120,
"name": "Samuel"
},
{
"leadId": 122,
"name": "Gabriel",
}
Need resulting array as
arr3 = [
{
"leadId": 129,
"name": Joseph
},
{
"leadId": 120,
"name": Samuel
}
]
Upvotes: 0
Views: 1755
Reputation: 461
UPDATED - Including DataWeave 1 and 2
I'm not sure exactly how you have your arrays stored (in the payload or other variables etc), but the below script should give you enough to go on. Comparison is done based on the leadId
only.
The transform actually works across both DW1 and DW2, you just need to change the header.
Mule 3 and DW1
%dw 1.0
%output application/json
---
payload.array2 filter (not (payload.array1.leadId contains $.leadId))
Mule 4 and DW2
%dw 2.0
output application/json
---
payload.array2 filter (not (payload.array1.leadId contains $.leadId))
Input
{
"array1": [
{
"leadId": 127,
"playerId": 334353
},
{
"leadId": 128,
"playerId": 334354
},
{
"leadId": 123,
"playerId": 43456
},
{
"leadId": 122,
"playerId": 43458
}
],
"array2": [
{
"leadId": 127,
"name": "James"
},
{
"leadId": 129,
"name": "Joseph"
},
{
"leadId": 120,
"name": "Samuel"
},
{
"leadId": 122,
"name": "Gabriel"
}
]
}
Output
[
{
"leadId": 129,
"name": "Joseph"
},
{
"leadId": 120,
"name": "Samuel"
}
]
Upvotes: 1