For this purpose we can declare Functions in the Header and refer it from DataWeave body. Here, I am using following JSON input:
{
"productInfo": [
{
"Id": "1",
"Revisions": [
{
"startDate": "20160101",
"endDate": "20160131"
},
{
"startDate": "20160201",
"endDate": "20160229"
}
]
},
{
"Id": "2",
"Revisions": [
{
"startDate": "20160301",
"endDate": "20160330"
},
{
"startDate": "20160401",
"endDate": "20160430"
}
]
}
]
}
And test it using this script:
%dw 1.0
%output application/json
%var filterStartDate = "20160101" //flowVars.inputDate["startDate"]
%var filterEndDate = "20160130" //flowVars.inputDate["endDate"]
%function evaluateRevisions(revisions) (
(revisions map {
evaluated: ($.startDate <= filterStartDate) and ($.endDate >= filterEndDate)
})
)
---
payload.productInfo map using (productInfo = $) {
filteredProduct: productInfo,
found: evaluateRevisions($.Revisions).evaluated contains true
} filter $.found
I can get the productInfo based on input date (startDate and endDate):
[
{
"filteredProduct": {
"Id": "1",
"Revisions": [
{
"startDate": "20160101",
"endDate": "20160131"
},
{
"startDate": "20160201",
"endDate": "20160229"
}
]
},
"found": true
}
]