Hello friends,

In this article we will see one of the common and challenging real-time use-case in IT industry during application development where we require to compare between arrays. These arrays could be from two different source or api responses.

Here we will see how we can compare two arrays results/payloads and get required results. Also we will see the complete example of our use-case.

To achieve this we will see how we can use the combination of some dataweave functions and how we can utilise these functions for our use-case.

  • flatMap
  • filter
  • contains
  • partation


There are many ways to achieve these functionality. Here, I have tried to show you two different methods to achieve this.


Method I:


flatMap

flatMap(Array<T>, (item: T, index: Number) -> Array<R>): Array<R>
It comes under dw::Core package from dataweave library.

Iterates over each item in an array and flattens the results. It produces output as Array. 

Instead of returning an array of arrays (as map does when you iterate over the values within an input like [ [1,2], [3,4] ]), flatMap returns a flattened array that looks like this: [1, 2, 3, 4]. flatMap is similar to flatten, but flatten only acts on the values of the arrays, while flatMap can act on values and indices of items in the array.

%dw 2.0
output application/json indent=false
[ [“a”:1,“b”:0], [“c”:0.1,“d”:7] ] flatMap (value, index) -> value
——————————————————————-
Output:
[{“a”: 1},{“b”: 0},{“c”: 0.1},{“d”: 7}]

filter

filter(Array<T>, (item: T, index: Number) -> Boolean): Array<T>

It comes under dw::Core package from dataweave library. It produces output as array.

Iterates over an array and applies an expression that returns matching values. The expression must return true or false. If the expression returns true for a value or index in the array, the value gets captured in the output array. If it returns false for a value or index in the array, that item gets filtered out of the output. If there are no matches, the output array will be empty.


%dw 2.0
output application/json
[9,2,3,4,5] filter (value, index) -> (value > 4)
————————————————-
Output:
[
9,
5
]


Complete example below by using flatMap and filter function:


%dw 2.0
output application/json
var filterCriteria = [
{
“IDENTITY”: “D40000”,
“NM”: “Delta”,
“CODE”: “D12”
},
{
“IDENTITY”: “C30000”,
“NM”: “Charlie”,
“CODE”: “C11”
}
]
var responsePayload= [
{
“CODE”: “A11”,
“NAME”: “Alpha”,
“ID”: “C10000”
},
{
“CODE”: “B12”,
“NAME”: “Bravo”,
“ID”: “B20000”
},
{
“CODE”: “C11”,
“NAME”: “Charlie”,
“ID”: “C30000”
},
{
“CODE”: “D12”,
“NAME”: “Delta”,
“ID”: “D40000”
},
{
“CODE”: “E12”,
“NAME”: “Echo”,
“ID”: “E50000”
}
]
filterCriteria flatMap(v) -> (
responsePayload filter (v.IDENTITY == $.ID and v.NM == $.NAME and v.CODE == $.CODE)
)
—————————————————————————————————————
Output:
[
{
“CODE”: “D12”,
“NAME”: “Delta”,
“ID”: “D40000”
},
{
“CODE”: “C11”,
“NAME”: “Charlie”,
“ID”: “C30000”
}
]

———————————————————————————————————————-

Method II:


Now, let use see the 2nd way to compare array by using contains and partition function. Here the input arrays can be from two different source or api responses. Let’s see how we can compare and filter the incoming arrays by using these two useful function.

contains

contains(Array<T>, Any): Boolean

It comes under dw::Core package from dataweave library. It returns true if an input contains a given value, false if not.

This version of contains accepts an array as input. Other versions accept a string and can use another string or regular expression to determine whether there is a match.



partation

partition(Array<T>, (item: T) -> Boolean): { success: Array<T>, failure: Array<T> }

It comes under dw::core::Arrays package from dataweave library.

It separates the array into the elements that satisfy the condition from those that do not.
Elements that satisfies conditions will come under “success” array partation and which it don’t satisfy comes “failure” array partation. So, you can retrieve the matching results under “success” partation node.

Note: Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later.


%dw 2.0
import partition from dw::core::Arrays
output application/json
var responsePayload = [
{
“CODE”: “A11”,
“NAME”: “Alpha”,
“ID”: “C10000”
},
{
“CODE”: “B12”,
“NAME”: “Bravo”,
“ID”: “B20000”
},
{
“CODE”: “C11”,
“NAME”: “Charlie”,
“ID”: “C30000”
},
{
“CODE”: “D12”,
“NAME”: “Delta”,
“ID”: “D40000”
},
{
“CODE”: “E12”,
“NAME”: “Echo”,
“ID”: “E50000”
}
]


var filterCriteria = [
{
“IDENTITY”: “D40000”,
“NM”: “Delta”,
“CODE”: “D12”
},
{
“IDENTITY”: “C30000”,
“NM”: “Charlie”,
“CODE”: “C11”
}
]
responsePayload partition (e) -> filterCriteria contains {IDENTITY:e.ID,NM:e.NAME,CODE:e.CODE}
————————————————————————————————————————–

Output
{
success: [
{
“CODE”: “C11”,
“NAME”: “Charlie”,
“ID”: “C30000”
},
{
“CODE”: “D12”,
“NAME”: “Delta”,
“ID”: “D40000”
}
],
“failure”: [
{
“CODE”: “A11”,
“NAME”: “Alpha”,
“ID”: “C10000”
},
{
“CODE”: “B12”,
“NAME”: “Bravo”,
“ID”: “B20000”
},
{
“CODE”: “E12”,
“NAME”: “Echo”,
“ID”: “E50000”
}
]
}

You can choose any of these methods while doing comparison of arrays for your use-cases.

Happy Learning 🙂

By Manish Kumar

I am having around 10 years of IT experience in Integration Architecture, Requirement gathering, Effort Estimation, Application Design\Development\Testing and Deployment including 5+ years of experience in MuleSoft ESB and Hybrid Integrations. DevOps and Cloud Integration is my area of interest.

Leave a Reply

Your email address will not be published. Required fields are marked *