Remove null value rows from payload in dwl 1.0 mule 3 - mule

I want to remove null values rows from the payload. skipNullOn is working if the output type is application/json but it's not working with application/java output and getting error like "Option skipNullOn is not valid. Valid options are: ". This is expected but I want a solution where I can remove/skip keys with null values with Java output.
Input:
{
"date1": "2021-02-121",
"date2": "2021-02-12 00:00:00",
"date3": null
}
Expected Output:
{
"date1": "2021-02-12",
"date2": "2021-02-12T00:00:00",
}
Script:
%dw 1.0
%output application/java skipNullOn="everywhere"
%function transformDate(dt) (dt as :date {format: "dd/MM/yyyy"} as :string {format: "yyyy-MM-dd"} when ((sizeOf (dt)) == 10) otherwise null)
%function changeDateTime(cdt) (cdt as :localdatetime {format: "dd/MM/yyyy HH:mm:ss"} as :string {format: "dd/MM/yyyy HH:mm:ss"})
%function isEmpty(value) (value!=null and value!="")
---
{
(date1: transformDate(payload.date1)) when (isEmpty(payload.date1)),
(date2: changeDateTime(payload.date2) when (isEmpty(payload.date2)),
(date3: changeDateTime(payload.date3) when (isEmpty(payload.date3))
}

Related

add 2hr in datetime field in mule 3

I'm trying to add two hours in datetime but always getting this error."Cannot coerce a :string to a :datetime, caused by :Text '2021-04-05 21:29:55' could not be parsed at index 2"
Please give solution.
dataweave code:
%dw 1.0
%output application/json
---
{
dateA: "2021-04-05 21:29:55" as :datetime {format: "dd/MM/yyyy HH:mm:ss"} + |PT2H|
}
expected output:
{
dateA: "2021-04-05 23:29:55"
}
%dw 1.0
%output application/json
---
{
dateA: (("2021-04-05 21:29:55" as :localdatetime {format: "yyyy-MM-dd HH:mm:ss"}) + |PT2H|) as :localdatetime {format: "yyyy-MM-dd HH:mm:ss"}
}

ignore invalid or null date field in dwl 1.0 mule 3

I'm trying to validate the date and datetime in input payload. I've to validate the request and remove the field if the date or datetime format is not correct or null. In case of correct format or null value it's working as expected but in case of wrong format it's throwing error "Cannot coerce a :string to a :localdatetime"
How can i fix this in dwl 1.0 (mule3)
Input:
{
"date1": "2021-02-12 00:00:00",
"date2": "2021-02-12",
"date3": "2021-02-121",
"date4": "2021-024-123 00:00:00",
"date5": "",
"date6": null
}
Expected Output:
{
"date1": "2021-02-12 00:00:00",
"date2": "2021-02-12",
}
code:
%dw 1.0
%output application/java
%function transformDate(dt) (dt as :date {format: "dd/MM/yyyy"} as :string {format: "yyyy-MM-dd"})
%function changeDateTime(cdt) (cdt as :localdatetime {format: "dd/MM/yyyy HH:mm:ss"} as :string {format: "yyyy-MM-dd'T'HH:mm:ss"})
%function isEmpty(value) (value!=null and value!="")
---
{
(date1: changeDateTime(payload.date1) as :date) when (isEmpty(payload.date1)),
(date2: transformDate(payload.date2) as :localdatetime) when isEmpty(payload.date2),
(date3: transformDate(payload.date3) as :localdatetime) when isEmpty(payload.date3),
(date4: changeDateTime(payload.date4) as :localdatetime) when isEmpty(payload.date4),
(date5: transformDate(payload.date5) as :localdatetime) when isEmpty(payload.date5),
(date6: transformDate(payload.date6) as :localdatetime) when isEmpty(payload.date6)
}
The problem is that when trying to parse the input dates with patterns that are not a match DataWeave throws an error:
Message : Exception while executing:
%function changeDateTime(cdt) (cdt as :localdatetime {format: "dd/MM/yyyy HH:mm:ss"} as :string {format: "yyyy-MM-dd'T'HH:mm:ss"})
^
Cannot coerce a :string to a :localdatetime, caused by :Text '2021-02-12 00:00:00' could not be parsed at index 2.
In DataWeave 2/Mule 4 you have the try() function to test if some expression fails. Unfortunately there is no equivalent in DataWeave 1/Mule 3. As alternatives you can use one of these methods:
Isolate each expression that could fail into a separate script and encasulate it into an error handler that can catch the expression.
Use the method described in this KB article to guard each pattern conversion with a condition and try to test if the pattern will match before attempting it. Example (dateStr) -> dateStr as :localdatetime {format: "E, d LLL u H:m:s O"} when (dateStr contains /^[A-z][A-z][A-z],/) otherwise (dateStr as :localdatetime {format: "E LLL d H:m:s u"} when (dateStr contains /^[A-z][A-z][A-z]\s/) otherwise dateStr as :localdatetime {format: "cccc, d-LLL-u H:m:s O"})

date operations in mule esb using data weave

I have requirement like where need to find difference in between two dates using dataweave, Both input and output is XML format.
both the date formats are yyyy.mm.dd and output date format must be like mm.dd.yy or mm.dd.yyyy.
Please assist me, thanks
You can format dates like this (example):
yourInputDate as :localdatetime {format: "yyyy-MM-dd'T'HH:mm:ss"})
you can add and subtract dates, another example how i use this in a project with variables:
%var stamp = (now as :localdatetime {format: "yyyy-MM-dd'T'HH:mm:ss"})
%var dayDiff = ("P" ++ (stamp.dayOfWeek - 1) ++ "D") as :period
%var firstDateWeek = (stamp - dayDiff) as :localdatetime {format: "yyyy-MM-dd"}
docs here: https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-types#datetime
In Dataweave, you can convert a string to date format and then just subtract them
Input
<dates>
<startDate>2007.05.01</startDate>
<endDate>2017.02.15</endDate>
</dates>
Transform
%dw 1.0
%var startDate = payload.dates.startDate as :date {format: "yyyy.MM.dd"}
%var endDate = payload.dates.endDate as :date {format: "yyyy.MM.dd"}
%output application/json
---
{
difference: startDate - endDate
}
Output
{
"difference": "P9Y9M14D"
}
9 years, 9 months, 14 days
You can create a global function with Java/Groovy and use DateTime object to find the difference and return from that function. Now you can use that function inside your dataweave.
Subtracting two Dates in dataweave. Format the date according to your requirement
%dw 1.0
%output application/json
---
{
a: |23:59:56-03:00| - |22:59:56-00:00|,
b: |2003-10-01| - |2002-09-23|
}
{
"a": "PT-4H",
"b": "P-1Y-8D"
}

how to convert yyyymmdd into mm-dd-yyyy in dataweave

I have two string 1 represent date and 2nd time. have to convert same into date format in Mule-dataweave
input :-
s1= 20161228(yyyymmdd),
s2= 1608(hhmm)
output :-
12-28-2016 16:08:00(mm-dd-yyyy hh:mm:ss) in date format.
Any Help?
This should work
%dw 1.0
%output application/java
%var s1= 20161228 // (yyyymmdd),
%var s2= 1608 //(hhmm)
---
output : (s1 ++ s2) as :localdatetime {format:"yyyyMMddHHmm"} as :string {format:"MM-dd-yyyy HH:mm:ss"}
Hope this helps.
{ "data":payload.date as {format:"MM/DD/YY"}as :string{format:"dd/mm/yy"} }
You can convert as given below.
DATUM: input as :localdatetime {format: "yyyymmdd"} as :string {format: "yyyyMMdd"},
DATUM: now as :string {format: "mm-dd-yyyy"}
For more about date formats see the below link.
https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-types#dates

DateWeaver date field conversion not working - Mule

In DataWeaver documentation 10.8. Changing the Format of a Date https://developer.mulesoft.com/docs/dataweave#_date_time_operations
Below is the transform
%dw 1.0
%output application/json
%type mydate = :string { format: "YYYY/MM/dd" }
---
{
formatedDate1: |2003-10-01T23:57:59| as :mydate,
formatedDate2: |2015-07-06T08:53:15| as :mydate
}
In the dataweaver preview it is looking fine as expected response ( Changed the date format).
I'm taking response in file component, But it is not converting the date in the format mentioned( Also kept logger right after the dataWeaver, not an expected response).
Response getting as below
{
"formatedDate1": "2003-10-01T23:57:59",
"formatedDate2": "2015-07-06T08:53:15"
}
I have other query, here we are hardCoding the date inside the weaver. If suppose we are taking the date field from Input parameter does we need to wrap the field inside ||. Example as below, will it work
%dw 1.0
%output application/json
%type mydate = :string { format: "YYYY/MM/dd" }
---
{
formatedDate1: |payload.dateField1| as :mydate,
formatedDate2: payload.dateField1 as :mydate
}
The above seems not to work for me. Please let me know the correct usage.
Thanks in advance
Try this:
%dw 1.0
%output application/json
%type mydate = :date { format: "yyyy/M/d" }
---
{
formatedDate1: |2003-10-01T23:57:59| as :mydate,
formatedDate2: |2015-07-06T08:53:15| as :mydate
}
Output:
{
"formatedDate1": "2003-10-01",
"formatedDate2": "2015-07-06"
}
The difference is the datatype from :string to :date::
%type mydate = **:date** { format: "yyyy/M/d" }
It seems the result doesn't change to /. This is probably a bug.
%dw 1.0
%output application/json
%type mydate = :string { format: "YYYY/M/d" }
---
{
formatedDate1: |2003-10-01T23:57:59| as :mydate,
formatedDate2: |2015-07-06T08:53:15| as :mydate
}
Try this