0
votes

I have an object of a similar structure. I need to filter the ProductInfo objects on start date and end date, and it can be selected even if any one of the revision dates fall within the interval. I have the user input , start date and end date as a hash

{
productInfo : [
{
Id: XXX,
Revisions:[
{
startDate : yyyy
endDate : zzz
}
{
 startDate : yyyy
endDate : zzz
}
]
and more productInfo objects.....
]}

map in a flow variable.

I cannot share my configuration. Can anyone help me with the dataWeave syntax for the above scenario.

Thanks

1
{ productInfo : [ { Id: XXX, Revisions:[ { startDate : yyyy endDate : zzz } { startDate : yyyy endDate : zzz } ] and more productInfo objects..... ]} - Satheesh Kumar

1 Answers

0
votes

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
  }
]