How to check NaN in mule - mule

How to check NaN value using xpath3 in mule.
I tried to do this but not getting result correct
#[xpath3('total',payload,'NUMBER')!='NaN'?xpath3('total',payload,'NUMBER'):0]
But not working
How can I check NaN value in mule?

Try using is or instance of, for example #[xpath3('total',payload,'NUMBER') is java.lang.Number ? xpath3('total',payload,'NUMBER') : 0]
Based on further testing, by using the following example:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<total>100</total>
</data>
We can utilize isNaN() method, because the payload is a java.lang.Double. Therefore we can use the following MEL: #[xpath3('//total',payload,'NUMBER').isNaN() ? 0 : xpath3('//total',payload,'NUMBER')]
It can be tested through the following simple flow:
<flow name="simpleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<logger message="#[xpath3('//total',payload,'NUMBER').isNaN() ? 0 : xpath3('//total',payload,'NUMBER')]" level="INFO" doc:name="Logger"/>
</flow>
Change the value of total field with a non numeric for testing purpose, e.g.: <data><total>hundred</total></data>

Related

How to concatenate in mule logger?

I need to concatenate text with json payload in logger component. I have tried below ways but no luck
<logger level="INFO" doc:name="Logger" doc:id="38de876a-a64f-4d83-86a1-ef4cbbda167c" message="#['payload is:' + payload]"/>
Even i don't see any transformers like 'object to string converter' in mule 3.
Please suggest syntax for mule 4
Try separating the text from your dataweave
i.e.
<logger level="INFO" doc:name="Logger" mesage="Payload is: #[payload]" doc:id="38de876a-a64f-4d83-86a1-ef4cbbda167c" />
All the various transformers were removed in Mule 4 due to the payload always being "accessible". That is, regardless of the payload type (XML, JSON, Java, CSV...) you can access fields through payload.{fieldname}. In Mule 3.x the payload had to be coerced to a Java object to allow that. You can explicitly set the output type of any dataweave expression, so you can also try:
mesage="Payload is: #[output application/java --- payload]"
It is working with below syntax
<logger level="INFO" doc:name="Logger" doc:id="38de876a-a64f-4d83-86a1-ef4cbbda167c" message="#['payload is:' ++ payload]"/>
I had the same issue and the below worked for me...
<logger level="INFO" doc:name="Logger" doc:id="35d4566e-02ba-495a-bd40-c30aa5a90413" message="#['Get Accounts Response Paylaod : #[payload]']"/>

JSON parsing for a particular element

Below xpath expression looks for AccountNo element anywhere in whole xml document.
#[xpath3('//AccountNo').text]
I am after similar expression to get AccountNo from a json request if present anywhere.
Below are example xml request containing AccountNo.
<Account>
<AccountName>John</AccountName>
<AccountNo>4234324</AccountNo>
</Account>
<Order>
<OrderId>34234242</OrderId>
<ServiceOrder>
<AccountNo>231232</AccountNo>
<ServiceOrderId>54654698787</ServiceOrderId>
</ServiceOrder>
<ServiceOrder>
<AccountNo>231232</AccountNo>
<ServiceOrderId>78979879797</ServiceOrderId>
</ServiceOrder>
</Order>
Thanks in advance for any help.
To get the value from a JSON payload element in Mule flow you first need to convert the json payload to object as below.
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
Then fetch the json payload element as below. In below code I am fetching the value of RecordId from json payload.
<set-variable variableName="RecordID" value="#[payload.DocProcessingMessage.UploadEfnolDocument.RecordId]" doc:name="Variable_RecordID"/>
Once you are done, Convert the payload back to JSON for further processing.
<json:object-to-json-transformer doc:name="Object to JSON"/>
First convert xml payload to Json then process the payload using transformer
<json:xml-to-json-transformer doc:name="XML to JSON" mimeType="application/json"/>
<set-payload value="{ "AccountNo": "#[json:Account:AccountNo]"}" mimeType="application/json" doc:name="payload"/>

Mule Server 3.6 > Anypoint Studio > Raw JSON as POST data

Still learning Mulesoft's Anypoint Studio... I am confused as how will I be able to access raw JSON POST data via the HTTP Listener then use the Choice flow control to execute conditions based on a value from a given JSON index. Anyone can show/tell me how to do this?
The JSON HTTP body will automatically become the payload of your message in Mule probably represented as Stream.
Just for demo purposes, try logging the payload after your http:listener using:
<object-to-string-transformer />
<logger level="INFO" message="#[payload]" />
There best way to query JSON is to transform it to a Map suing the JSON module transformers.
<json:json-to-object-transformer returnClass="java.util.HashMap" />
And then query it using MEL like standard MVEL or Java syntax.
For a JSON document like: {"person" : {"name" : "bob"}}
<logger message="#[payload.person.name]" level="INFO" />
You can use these expressions in your choic router also:
<choice>
<when expression="#[payload.person.name == 'bob']">
do something ...
</when>
</choice>

How to Extract the Flow Name and MessageProcessor Name using MEL - MULE ESB

I'm not sure, how can we extract the flow-name and message-processor Name through MEL. For example I have multiple message processor. For logging, i need to extract the flow Name and Message-processor, so that I can find out transaction has crossed this particular flow and its message processor. Is there any simple way to find out. Please guide me. Please find the screenshot below. Here i need to Extract - set payload and its flowName (flow1)
Thanks in advance.
For mule 3.8+ version onwards #[flow.name] don't work.
Use #[mule:context.serviceName] expression in logger or component to extract the name of the flow
I know this post is old but I have been trying to find a way to do this in MEL for error handling emails.
For the flow name you can use #[exception.event.flowConstruct.name]
for the failing message processor you can use #[exception.failingMessageProcessor].
Both of these work in MEL without the need to use an flowVar.
Please note however, that the failing processor does not always come back with a value but comes back with null, I'm not sure why.
You can extract the flow-name with MEL : #[flow.name]
<flow name="name" doc:name="name">
<http:inbound-endpoint address="http://localhost:8090/resources" doc:name="HTTP" />
<logger message="name of flow: #[flow.name]" level="INFO" doc:name="Logger"/>
<set-payload value="name" doc:name="Set Payload"/>
</flow>
or
flowConstruct.getName() in a Message Processor
Two ways to acthive this (from current flow name)
First one is -
<logger message="Current flowName: #[flow.name]" level="INFO" doc:name="Logger"/>
and the second one is -
<logger message="Current flowName: #[context:serviceName]" level="INFO" doc:name="Logger"/>

How's Mule <object-to-string-transformer> different than message.getPatyloadAsString()

I've a soap service flow which gets inbound request through <cxf:proxy-service>. I have a set payload right after it to get payload as String.
Here is my flow:
<flow name="soapService">
<http:inbound-endpoint address="${service.address}" exchange-pattern="request-response">
<cxf:proxy-service wsdlLocation="classpath:service.wsdl" namespace="http://pennmutual.com/services/mvi" service="MVIService" enableMuleSoapHeaders="false"/>
</http:inbound-endpoint>
<set-payload value="#[message.payloadAs(java.lang.String)]"/>
.
.
.
</flow>
<set-payload value="#[message.payloadAs(java.lang.String)]"/> works but if I replace it with <object-to-string-transformer>, it doesn't work.
How are these 2 inherently different?
UPDATE:
On replacing <set-payload value="#[message.payloadAs(java.lang.String)]"/> with <object-to-string-transformer> in my above flow, #[payload] gives com.ctc.wstx.sr.ValidatingStreamReader#429eb61a instead of the atcual XML
message.payloadAs(java.lang.String) relies on Mule's auto-transformation framework: I supposed it picks <xml:dom-to-xml-transformer> instead of <object-to-string-transformer> as the most appropriate transformer.