Mule - Converting String to Date - mule

I have a csv file which looks like this :
I am iterating the CSV file using For each after converting it to Java ArrayList using Dataweave.
Now, I need to convert one of the elements which is Date from String to Oracle Timestamp. Please help with your thoughts. The source code looks like this :
<flow name="testFlow">
<file:inbound-endpoint path="src/main/resources/input"
moveToDirectory="src/main/resources/output" connector-ref="File"
responseTimeout="10000" doc:name="File"/>
<dw:transform-message metadata:id="4e33c7ff-c33b-4c78-be34-79a154aa16df"
doc:name="Transform Message">
<dw:input-payload doc:sample="sample_data\list_csv.csv"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-payload>
</dw:transform-message>
<logger level="INFO" doc:name="Logger"/>
<foreach collection="#[payload]" doc:name="For Each">
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</foreach>
</flow>
Flow design looks like this :

Basically what you need to do is to convert the date field which is a string in csv to date format. To do it just provide the format of the incoming data field and it will work for the below input example(csv is comma separated) :-
foo,bar,Name,Date
foo1,baar1,Name1,15-01-2016 12:08
foo2,baar2,Name2,15-01-2016 12:08
you can try below dw :-
%dw 1.0
%output application/java
---
payload map {
foo : $.foo,
bar : $.bar,
Name : $.Name,
Date : $.Date as :date {format: "dd-MM-yyyy hh:mm" }
}

Related

Add one more element to the object array inside for each loop in mule 3

I have Input as below
[{Name=ABC, ID=123},{Name=XYZ, ID=345}]
I would iterate over this collection in a for-each loop and add one more element Age to each object.
My expected output would be like
[{Name=ABC, ID=123, Age=23},{Name=XYZ, ID=345, Age=24}]
Any help would be highly appreciated. Thanks in advance.
HTH..
%dw 2.0
output application/json
var inp = [
{
"Name":"ABC",
"ID":"123"
},
{
"Name":"XYZ",
"ID":"345"
}
]
---
inp map {
($),
age: (23 + ($$)) // or your logic to derive age
}
It was not possible with DW as because payload inside for-each was modified. So I tried using Expression component. Below is my configuration XML.
<flow name="add-one-more-element-to-the-original-payload">
<poll doc:name="Poll">
<fixed-frequency-scheduler frequency="30" timeUnit="SECONDS"/>
<logger message="Pooling Started" level="INFO" doc:name="Log"/>
</poll>
<dw:transform-message doc:name="Transform Payload">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
%var collection = [{"Name":"ABC","ID":123},{"Name":"XYZ","ID":345}]
---
collection]]></dw:set-payload>
</dw:transform-message>
<set-variable variableName="outputList" value="#[new java.util.ArrayList()]"
doc:name="Set Variable"/>
<foreach doc:name="For Each" collection="#[payload]">
<dw:transform-message doc:name="CurrentPayload">
<dw:set-variable variableName="currentPayload"><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-variable>
</dw:transform-message>
<flow-ref name="get-age-subflow" doc:name="Get Age"/>
<expression-component doc:name="Expression"><!
[CDATA[flowVars.currentPayload.Age=payload.Age;]]></expression-component>
<expression-transformer expression="#
[flowVars.outputList.add(flowVars.currentPayload)]" doc:name="Expression"/>
</foreach>
<logger message="#[flowVars.outputList]" level="INFO" doc:name="Logger"/>
</flow>
<sub-flow name="get-age-subflow">
<dw:transform-message doc:name="Transform Age">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
Age:24
}]]></dw:set-payload>
</dw:transform-message>
</sub-flow>

Mule if flowVar used in Dataweave Transformer payload converted to Map

I have an issue with a Mule Dataweave Transformer getting an error, this only occurs if :-
An enricher is present wrapping a json:json-to-object-transformer
<enricher target="#[flowVars.myVar]" doc:name="Message Enricher">
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
</enricher>
A flowVar is involved in the Dataweave Transformer
The exception occurs on the 2nd dataweave :-
ERROR 2017-08-15 09:09:52,239 [amqpReceiver.02] org.mule.exception.RollbackMessagingExceptionStrategy:
********************************************************************************
Message : Exception while executing:
{bar=1, foo=0}
^
Unexpected character 'b' at index 1 (line 1, position 2), expected '"'
Payload : com.mulesoft.weave.reader.ByteArraySeekableStream#39e7a0d3
Element : /subscribe-to-changeFlow4/processors/4 # voa-009-mule-authorisation-search-api:test3.xml:58 (Transform Message)
Element XML : <dw:transform-message doc:name="Transform Message">
<dw:input-payload mimeType="application/json"></dw:input-payload>
<dw:set-payload>%dw 1.0%output application/json---{field1: payload.foo,field2: payload.bar,field3: flowVars.myFlowVar}</dw:set-payload>
</dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
com.mulesoft.weave.reader.json.JsonReaderException: Unexpected character 'b' at index 1 (line 1, position 2), expected '"'
at com.mulesoft.weave.reader.json.JsonTokenizer.fail(JsonTokenizer.scala:193)
at com.mulesoft.weave.reader.json.JsonTokenizer.require(JsonTokenizer.scala:190)
at com.mulesoft.weave.reader.json.JsonTokenizer.readString(JsonTokenizer.scala:74)
at com.mulesoft.weave.reader.json.JsonTokenizer.readObjectMembers(JsonTokenizer.scala:146)
at com.mulesoft.weave.reader.json.JsonTokenizer.com$mulesoft$weave$reader$json$JsonTokenizer$$readObject(JsonTokenizer.scala:140)
at com.mulesoft.weave.reader.json.JsonTokenizer$$anonfun$readValue$1.apply$mcV$sp(JsonTokenizer.scala:37)
What happens is as soon as a flowVar is added into the 2nd data weave, then the payload seems to show as a Map rather than JSON.
The enricher with nested json-to-object-transformer is required to take a payload and allow the JSON to be accessed. If the enricher is removed it works fine
The seek(0) is required to reset the stream after the json-to-object-transformer as Answered in my last question
if the flowVar is removed from the 2nd dataweave it works fine
Test Case
<flow name="subscribe-to-changeFlow4" processingStrategy="synchronous">
<amqp:inbound-endpoint numberOfChannels="1" ref="authorisationChangeQueueDef4" responseTimeout="10000" doc:name="authorisation-change-consumer" />
<dw:transform-message doc:name="Transform Message">
<dw:set-payload>
<![CDATA[%dw 1.0
%output application/json
---
{
foo:0,
bar:1
}]]>
</dw:set-payload>
</dw:transform-message>
<enricher target="#[flowVars.myVar]" doc:name="Message Enricher">
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
</enricher>
<expression-component doc:name="Expression">
<![CDATA[payload.seek(0);]]>
</expression-component>
<set-variable variableName="myFlowVar" value="1234" doc:name="Variable" />
<dw:transform-message doc:name="Transform Message">
<dw:input-payload mimeType="application/json"/>
<dw:set-payload>
<![CDATA[%dw 1.0
%output application/json
---
{
field1: payload.foo,
field2: payload.bar,
field3: flowVars.myFlowVar
}
]]>
</dw:set-payload>
</dw:transform-message>
<logger level="INFO" message="#[message.payloadAs(java.lang.String)]" doc:name="Logger all "/>
</flow>
This is probably a bug in Mule and you can raise it in Mule jira
Which Mule runtime version are you using?
I tested locally and found this issue with Mule runtime 3.8.0 and Mule runtime 3.8.1:
but worked fine with Mule runtime 3.8.2 and Mule 3.8.5
Also tested with Mule 3.7.3 locally and it worked fine as below:-
I had similar problem, the DW processor messes up the payload. So I added an Object-to-String before DW transformer and that worked fine.

how to make skipNullOn dynamic in dataweave script?

Based on Boolean variable I want to get output. If variable is true null parameters should not visible in output and if false null parameters should be visible. How to make skipNullOn dynamic in mule dataweave script?
#swamy Thota not sure if you can do it in dataweave.
Alternate option i can think off is, use choice and 2 dataweaves, one will skip null and other will allow null.
Please use a flow variable , the use a choice and create two different mapping one for true condition and one for false.
It will be something as below
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:encryption="http://www.mulesoft.org/schema/mule/encryption" xmlns:http="http://www.mulesoft.org/schema/mule/http"
<flow name="file2fileFlow">
<file:inbound-endpoint path="D:\Tushar\Training\DataWeave\in" moveToDirectory="D:\Tushar\Training\DataWeave\out" responseTimeout="10000" doc:name="File"/>
<set-variable variableName="test" value="#['a']" doc:name="Variable"/>
<choice tracking:enable-default-events="true" doc:name="Choice">
<when expression="#[flowVars.test] == 'a'">
<dw:transform-message doc:name="If Value True">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
}]]></dw:set-payload>
</dw:transform-message>
</when>
<otherwise>
<dw:transform-message doc:name="If Value false">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
}]]></dw:set-payload>
</dw:transform-message>
</otherwise>
</choice>
<logger level="INFO" doc:name="Logger"/>
<file:outbound-endpoint outputPattern="output.txt" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
You can load dw script from file and dynamic replace in text script skipNullOn value. Then, call the MEL function #[dw(<script>, [<output type>] ]
More examles you can see there: https://support.mulesoft.com/s/article/ka4340000004GbvAAE/Dynamic-DataWeave-Script

DataWeave Transform Message transforms 8192 bytes when deployed to Cloudhub

I am receiving a JSON request with 3 attribute and one of the attribute is content of image which is base64 in string format. After receiving this request I am creating a Salesforce attachment using Salesforce connector.
Now when I test this via my local Anypoint Studio everything is fine, however after deploying to Cloudhub the fileContent (Image content) is getting truncated to 8192 bytes and creating the attachment as corrupted.
I can see API is receiving the content since message content length is 38820 (both local and Cloudhub), and it looks like message is getting lost after the Message Transformation, and code is as follows:
<processor-chain doc:name="Processor Chain">
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
accountId : payload.accountId,
name : 'MyProfilePicture',
ContentType: payload.contentType,
fileContent : payload.fileContent
}]]></dw:set-payload>
</dw:transform-message>
<set-session-variable variableName="accountId" value="#[payload.accountId]" doc:name="Session Variable - accountId"/>
<set-session-variable variableName="contentType" value="#[payload.ContentType]" doc:name="Session Variable - contentType"/>
<logger message="payload.fileContent Before Conversion size #[payload.fileContent.length()], data:#[payload.fileContent] " level="INFO" doc:name="Logger"/>
<set-session-variable variableName="fileContent" value="#[(org.apache.commons.codec.binary.Base64.decodeBase64(payload.fileContent))]" mimeType="binary/octet-stream" doc:name="Session Variable - fileContent"/>
<dw:transform-message metadata:id="1676949a-75ba-4e51-82ac-b5788a8e87dc" doc:name="Transform Message">
<dw:input-payload/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
[{
Name: 'MyProfilePicture',
ParentId: sessionVars.accountId,
ContentType: sessionVars.contentType,
Body: sessionVars.fileContent
}]]]></dw:set-payload>
</dw:transform-message>
<logger message="Payload after Transform Message: #[payload]" level="INFO" doc:name="Logger"/>
<sfdc:create config-ref="Salesforce__Cfg" type="Attachment" doc:name="Salesforce">
<sfdc:objects ref="#[payload]"/>
</sfdc:create>
</processor-chain>
Could anyone please help on this? why when I deploy to Cloudhub it is only processing 8192 bytes?
Thanks,
Thomas
This can be fixed by switching to the deprecated DataMapper for the huge base64 transforms.

Generating random numbers in mule dataweave

I have to concatenate randomly generated number with the field from request in dataweave.
NUMBR: "AA" ++ $.Load.Reference.*Reference ++ RandomNumber
How to achieve this in Mule Dataweave
Not sure what you can do in Datawevae to do this, but you can you set a Random number in a flowVariable and invoke it from your Dataweave script like so:
<set-variable variableName="random"
value="#[new java.util.Random().nextInt(100)]" doc:name="Variable" />
<dw:transform-message doc:name="Transform Message">
<dw:input-variable doc:sample="unknown.dwl" variableName="random" />
<dw:set-payload>
<![CDATA[%dw 1.0
%output application/dw
---
{
"data": ("22" as :number + flowVars.random)
} ]]>
</dw:set-payload>
</dw:transform-message>
You can also use Expression Component to assign it to Payload or variables and then concatenate
<flow name="random-numbersFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/random" allowedMethods="GET" doc:name="HTTP"/>
<expression-component doc:name="Expression"><![CDATA[payload = new java.util.Random().nextInt(100)]]></expression-component>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
data: payload
}]]></dw:set-payload>
</dw:transform-message>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
You can't do this in pure DataWeave but you could use two methods to generate the number elsewhere in the application:
You could call a global MEL function from DataWeave: https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation#global-mel-functions
You could call a flow that returns the value: https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation#expressions-that-call-external-flows
Just set a random value generated by Java into a Flow Variable
<set-variable variableName="Random_Variable" value="#[java.util.Random().nextInt(10)]" doc:name="Random Variable"/>
Then Use that Flow Variable in your Dataweave Transform.
<dw:transform-message doc:name="Transform Message" metadata:id="8098b24c-30c1-4e9e-a3ce-9e8aaaec7bd1">
<dw:input-variable mimeType="application/java" variableName="Random_Variable"/>
<dw:set-payload><![CDATA[%dw 2.0
%output application/json
---
{
NUMBR: "AA" ++ $.Load.Reference.*Reference ++ flowVars.Random_Variable
}]]></dw:set-payload>
</dw:transform-message>
In Mule 4 Dataweave 2 function random()
Returns a pseudo-random number greater than or equal to 0.0 and less than 1.0
MULE 4 DOC:
https://docs.mulesoft.com/dataweave/2.3/dw-core-functions-random
Example:
%dw 2.0
output application/json
{ price: random() * 1000 }