-1

FOllowing is the input that i have [{ "date": " " }, { "date": "2022-01-21" },{ "date": " " },{ "date": " " },{ "date": " " }] And the required output is as follows

[ { "Date": "2022-01-21" }, { "Date": "2021-01-21" }, { "Date": "2020-01-21" }, { "Date": "2019-01-21" }, { "Date": "2018-01-21" } ]

Thanks in advance!!

Nidhi
  • 1
  • What do you mean by decreasing the date when there is a null value exactly? Decreased with respect to what initial value? In your example the second element of the array has a value ("2022-01-21") and it is replaced by something else. – aled Mar 31 '22 at 11:33

1 Answers1

1

It should be obvious that there is no buil-in function in DataWeave to achieve this result. You can create a custom function for it. For example using a recursive function we can get the expected output from your input:

%dw 2.0
output application/json
fun decDates(a, nextDate)=
    [{date: nextDate}]
     ++         
    if (sizeOf(a) > 1) decDates(dw::core::Arrays::drop(a, 1), nextDate - |P1Y|) else []
---
decDates(payload, |2022-03-21|)

Output:

[
  {
    "date": "2022-03-21"
  },
  {
    "date": "2021-03-21"
  },
  {
    "date": "2020-03-21"
  },
  {
    "date": "2019-03-21"
  },
  {
    "date": "2018-03-21"
  }
]
aled
  • 21,330
  • 3
  • 27
  • 34