Skip to main content

My Input will get as below String like:

 

[{

"ComplteDate in 180 days"

}] 

 

The scenario like I need to filter the data from source based on field called "ComplteDate" using this date field need to fetch data with WhereClause =[ComplteDate]<= dateadd(dd,180,Cast(getdate() as date) )

Example payload in Data source :[

  {

    "PerformNumber": "C-81022",

    "Status": "In Progress",

    "ComplteDate": "2028-05-01"

  },

  {

    "PerformNumber": "C-81023",

    "Status": "In Progress",

    "ComplteDate": "2029-09-01"

  },

  {

    "PerformNumber": "C-81024",

    "Status": "In Progress",

    "ComplteDate": "2028-05-01"

  },

  {

    "PerformNumber": "C-81024",

    "Status": "In Progress",

    "ComplteDate": "2029-09-01"

  },

  {

    "PerformNumber": "C-81025",

    "Status": "In Progress",

    "ComplteDate": "2028-05-01"

  },

  {

    "PerformNumber": "C-81026",

    "Status": "In Progress",

    "ComplteDate": "2029-09-01"

  },

  {

    "PerformNumber": "C-81026",

    "Status": "In Progress",

    "ComplteDate": "2025-05-01"

  },

  {

    "PerformNumber": "C-81027",

    "Status": "In Progress",

    "ComplteDate": "2025-09-01"

  },

  {

    "PerformNumber": "C-81028",

    "Status": "In Progress",

    "ComplteDate": "2025-05-01"

  },

  {

    "PerformNumber": "C-81029",

    "Status": "In Progress",

    "ComplteDate": "2025-09-01"

  }

]

2 个回答
  1. 9月10日 15:35

    Hi Vya, 

     

    Here's the DataWeave 2.0 script for this filter (ComplteDate <= today + 180 days): 

     

    %dw 2.0 

    output application/json 

    var cutoffDate = (now() as Date) + |P180D| 

    --- 

    payload filter ( 

        (item.ComplteDate as Date {format: "yyyy-MM-dd"}) <= cutoffDate 

     

    Applied to your sample payload, this returns: 

     

    %dw 2.0 

    output application/json 

    var cutoffDate = (now() as Date) + |P180D| 

    --- 

    payload filter ( 

        (item.ComplteDate as Date {format: "yyyy-MM-dd"}) <= cutoffDate 

     

    Key points on how this works: 

     

    1. now() returns the current DateTime. Casting it to Date strips the time portion, so you're comparing pure dates, not datetime with time-of-day noise. 

    Reference:

    https://docs.mulesoft.com/dataweave/latest/dw-core-functions-now

     

     

    2. |P180D| is a Period literal representing 180 days, this is DataWeave's native way of expressing durations (Period type), introduced in DataWeave 2.4.0. Adding it directly to a Date value shifts the date forward by that period. 

    Reference:

    https://docs.mulesoft.com/dataweave/latest/dw-periods-functions-period

     

     

    3. item.ComplteDate as Date {format: "yyyy-MM-dd"}: your ComplteDate field comes in as a String ("2028-05-01"), so it needs an explicit cast to Date with the matching format before it can be compared against another Date value. Comparing a String directly against a Date will not work correctly. 

     

    4. filter iterates the array and keeps only elements where the condition evaluates true, standard DataWeave array function, no import needed for filter itself (only the Dates/Periods module functions need explicit import if you use more advanced ones like today() or the dw::core::Dates module functions). 

     

    If you'd rather use the more explicit Dates module (cleaner syntax for date-only arithmetic, avoids DateTime timezone quirks entirely): 

     

    %dw 2.0 

    import * from dw::core::Dates 

    output application/json 

    var cutoffDate = today() plusDays 180 

    --- 

    payload filter ((item.ComplteDate as Date {format: "yyyy-MM-dd"}) <= cutoffDate) 

     

    Reference:

    https://docs.mulesoft.com/dataweave/latest/dw-dates

     

     

    Either version works, the second is arguably more readable for pure date math since it avoids mixing DateTime and Date casting.

0/9000