0
votes

I have this example payload that I want to filter based on the datetime value :

[
    {
        "title" : "Article 1",
        "createdDateTime": "2021-04-04T18:41:00"
    },
    {
        "title" : "Article 2",
        "createdDateTime": "2021-04-04T18:49:00"
    },
    {
        "title" : "Article 3",
        "createdDateTime": "2021-04-04T18:55:00"
    }
]

I only want the articles that are created 15 mins ago so for example datetime now is "2021-04-04T19:00:00", I should only get the Article 2 and Article 3. Tried this expression but did not work :

%dw 2.0
output application/json
---
payload filter ((value) -> (value.createdDateTime as: datetime{"yyyy-MM-ddTHH:mm:ss"}  <= now()-|PT15M|))

Is there a better way to do this? Thanks!

1

1 Answers

1
votes

This solution uses a function which receives the list, the timestamp (you can use now()) and a period.

%dw 2.0
output application/json
fun filterPeriod(list, time, period) =
    payload filter ((value) -> (time - (value.createdDateTime 
        as LocalDateTime { format: "yyyy-MM-dd'T'HH:mm:ss"}))  as Number {unit: "milliseconds"} <= period  as Number {unit: "milliseconds"}  )
---
filterPeriod(payload, |2021-04-04T19:00:00|, |PT15M|)

Output:

[
  {
    "title": "Article 2",
    "createdDateTime": "2021-04-04T18:49:00"
  },
  {
    "title": "Article 3",
    "createdDateTime": "2021-04-04T18:55:00"
  }
]
``'