Why Date Field is not formatting in Data weave? - mule

Hi i have a Date field which is coming as
{
"noteDate": "2013-12-18T00:00:00"
}
My Dataweave function is
%dw 2.0
output application/json
---
payload.noteDate as String {"format": "uuuu-MM-dd"}
//formattedDate: |2020-10-01T23:57:59| as String {format: "uuuu-MM-dd"}, // This is in Documentation
The output i am getting is
"2013-12-18T00:00:00"
Expected Output is
"2013-12-18"
How i can do it.

%dw 2.0
output application/json
---
payload.noteDate as Date
That is coming in as a string, not as a date. JSON has no concept of dates.
If you wanted to treat it as a DateTime and then apply a string format you could convert it first (shown below) but just casting it to a Date will do the same.
%dw 2.0
output application/json
---
(payload.noteDate as DateTime) as String { format: "uuuu-MM-dd" }

Related

override skipNullOn="everywhere"

I have divided my dataweave script into modules, and I have used skipNullOn="everywhere" in the main dwl, so all the null values in all the modules are skipped. But, I don't want to skip the null values of a particular modules. How do I override(nullify) the skipNullOn="everywhere" for that particular module.
Input:
<XML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ABC xsi:nil="true"/>
<DEF/>
</XML>
dataweave code:
%dw2.0
output application/json skipNullOn="everywhere"
---
payload.XML
Expected Output(
json):
{
"ABC": ""
}
Getting Output(json):
{
}
Since I am answering it pretty late, I am not sure if this of much help to you. Anyways, you can have the list of nodes for which you want to skip the 'skipNullOn' check in a comma separated format in the property file. And then you try something similar as I have here below, which will help you iterate over all the nodes and then achieve the output as you desire:
%dw 2.0
output application/json skipNullOn="everywhere"
var toSkipNullOn='ABC,XYZ'
fun checkNull(key,val) = if((toSkipNullOn splitBy(',')) contains(key as String)) '' else null
---
payload.XML mapObject (v0, k0, i0) ->
{
(k0):checkNull(k0,v0)
}
In this example I have hardcoded the node names (ABC,XYZ) to the variable toSkipNullOn. Instead of that you'll have to read it from the property file as p('key-name') and assign it to toSkipNullOn.
You need to explicitly write the logic for that field, should be something like this
%dw2.0
output application/json skipNullOn="everywhere"
---
{
"ABC": if (payload.XML.ABC_val !=null ) else ""
}
You can try this workaround, to get the expected result. Use two dataweave
1st Dataweave (wherever you get "nil", syntax need to be updated as the below one)
%dw 2.0
output application/xml skipNullOn="everywhere"
ns xsi http://www.w3.org/2001/XMLSchema-instance
---
{
ABC #( xsi#nil:"true"): payload.ABC ,
DEF : payload.DEF
}
2nd Dataweave
%dw 2.0
output application/json
---
payload
You can then replace null with "" easily.

Mulesoft 4, How to get only the value of a tag from xml file

New to Mule4 and have a question.
I am posting a simple xml file using postman
<cust>
<act>1234</act>
</cust>
In my flow I have
Listener->TransformMessage->logger
Inside TransformMessage I have the following:
Added a new target called f1 as variable
%dw 2.0
output application/java
var myXML = payload
---
{
f1: myXML.cust.act
}
And in the logger I am printing the value using #[vars.f1] and the output I am getting is {f1=1234} which is right.
So my question is how can I only get the value “1234” out of it? Reason is let says I want to use this in a query, for example
Select name, address from account where accountNumber = ':vars.f1'
Thanks
The problem is that you are wrapping the result in an object with a field f1
%dw 2.0
output application/java
var myXML = payload
---
{
f1: myXML.cust.act
}
So what you should do is just return the value like
%dw 2.0
output application/java
var myXML = payload
---
myXML.cust.act

string to date conversion using dataweave

Input String: 201801
Output String format: 01.2018
I tried using following but it's not working, I also looked up for string to date convesrion/coercion table in "Type Coercion Table" https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-types#type-coercion-table. I did not find something to help me with this.
as :date {format: "yyyyww"} as :string {format: "ww.yyyy"}
Appreciate if anyone has any ideas to share.
If you don't mind a bit of string hackery you could just move the various parts of the input string around:
%dw 1.0
%output application/json
%var inputDate = "201801"
---
{
outputDate: inputDate[4..5] ++ "." ++ inputDate[0..3]
}
The output of that is
{
"outputDate": "01.2018"
}
The above isn't null safe though, if there is a chance that the input date string is empty, or if it is shorter than 6 characters you will get a runtime error. You could work around that by checking the date in the DW, something like
outputDate: (inputDate[4..5] ++ "." ++ inputDate[0..3]) when inputDate != null and ( sizeOf inputDate ) == 6 otherwise null

There is no variable named 'substring'

i want to Change the time "2017-08-22T17:10:12Z" into "20170822".
So i used this substring function but its throw errors continuously.
%dw 0.1
%output application/xml
---
po: {
var:payload.po.ordered_date,
Date: substring(var,2,3)
}
There is no function called substring in Dataweave instead use as given below.
%dw 0.1
%output application/xml
po: {
var:payload.po.ordered_date,
Date: substring(2..3)
}
Or you can define a Java function substring in global xml config file and use it as you have given above.
Variable declaration has syntax error. Also in dataweave substring doesnt work as mentioned. Please refer following code
%dw 0.1
%output application/xml
%var variable = payload.po.ordered_date
---
po: {
Date: variable as :localdatetime as :string {format:"yyyyMMdd"}
}
Hope this helps.
I use Groovy Script to retrieve the substring. Try like below.
def testString = 'ABC';
flowVars['subStringedString']= flowVars.testString.substring(0,2);
The above will give you AB.
You can access this substring as flowVars['subStringedString'] in Mule flow
There is no substring operator in Dataweave and the way you to do this is like var[1..3] is you want get 4 characters from 2nd position. Should be something on lines below
%dw 0.1
%output application/xml
---
po: {
var:payload.po.ordered_date,
Date: var[1..3]
}

How to parse yyyy-Mm-ddThh:mm:ss format in dataweave mule

I tried expression like below in mule dataweave but I am getting error.
pdate : "2017-06-22T12:45:55" as:datetime{format : "yyyy-Mm-dd'T'hh:mm:ss"} as :string{format: "MM/DD/yyyy"}
Can you please help on this if I missed anything to write.
try this:
%dw 1.0
%output application/json
---
{
pdate: "2017-06-22T12:45:55" as :localdatetime { format: "yyyy-MM-dd'T'HH:mm:ss" } as :string { format: "MM/dd/yyyy" }
}
changes compared to your version:
replace Mm with MM in first format string, thx #Dai
replace hh with HH in first format string
replace DD with 'dd' in second format string