JSON parsing for a particular element - mule

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"/>

Related

mule org.json.JSONObject returning property value as null though the json property does have value for it

I am using the below code to convert the input payload string to json in mule. The below code sometimes working and sometimes not. its not working on standalone and working on studio. Not able to nail down the exact cause for it. but based on the loggers that i see that the property value is coming null after the expression statement. i am suspecting this could be with the jar that's getting used here. i am still digging further on it.
<logger message="input: #[payload]" level="INFO" doc:name="Logger"/>
<set-payload value="#[payload.'data']" mimeType="application/json" doc:name="Set Payload" encoding="ISO-8859-2"/>
<logger message="createConnection: #[payload]" level="INFO" doc:name="Logger"/>
<expression-component doc:name="Expression"><![CDATA[String input = payload;
payload = new org.json.JSONObject(input);
]]></expression-component>
<logger message="before json to object: #[payload.con_id] #[payload.'con_id']" level="INFO" doc:name="Logger"/>
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
Input JSON:
data: {"name":"QA_tst2","description":"tst","con_id":10,"con_connection_id":null,
"verticalParam":[{"param_value":"abc","param_name":"Host"},{"param_value":"21","param_name":"Port"}],"CON_CATEGORY_NAME":"File"}
I don't think that notation will work for JSONObject, try using
payload.get('con_id')
as per the javadoc: https://stleary.github.io/JSON-java/org/json/JSONObject.html.
The reason this won't work with the notation you have tried, is that Mule supports that notation for Maps, and org.json.JSONObject does not implement java.util.Map. You could try using javax.json.JSONObject instead, which will support that notation.
I have figured out the current issue. if there is any logger added to fetch the properties from the payload right after the expression component then its screwing up further. if you just remove the logger that was added after the expression component then after json to object conversion, i am able to fetch the values. that solves the current issue. but i would like to understand the difference between fetching the properties #[payload.con_id] vs #[payload.'con_id']. i can start a separate conversation for the same.

How to convert org.glassfish.grizzly.utils.BufferInputStream to JSON in Mule?

On my first steps with Mule I'm writing a basic Http Proxy.
Currently I forward the request to the api server and what I'd like to do is reading the payload that I receive from it before responding to the client.
When I try to log it with #[payload] it prints
org.glassfish.grizzly.utils.BufferInputStream#2306df30
How can I print it properly in JSON format?
The full code:
<flow name="proxy">
<http:listener config-ref="http-lc-0.0.0.0-8081" path="![p['proxy.path']]" parseRequest="false"/>
<http:request config-ref="http-request-config" method="#[message.inboundProperties['http.method']]"
path="#[message.inboundProperties['http.request.path'].substring(message.inboundProperties['http.listener.path'].length()-2)]" parseResponse="false">
<http:request-builder>
<http:query-params expression="#[message.inboundProperties.'http.query.params']"/>
</http:request-builder>
<http:success-status-code-validator values="0..599" />
</http:request>
<logger doc:name="Logger" level="INFO" message="Payload #[payload]"/>
The payload after HTTP request is generally in stream format, ref:- https://docs.mulesoft.com/mule-user-guide/v/3.7/http-request-connector
There are two ways you can get the payload after http:request
1) <object-to-string-transformer doc:name="Object to String"/> after http:request
or
2) using a logger and use MEL expression <logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
Try #[message.payloadAs(java.lang.String)] which will log the expected output.
Hope this helps.
The http component will send as InputStream,So use byte array to string transformer after http component.If you just want to print you can use #[message.payloadAs(java.lang.String)] but you wanna do any operation just drag and drop a byte array to string transformer
Simplest way is to just use <object-to-string-transformer doc:name="Object to String"/> after http request component and then place a logger with #[payload].
One way to make sure that you get proper JSON response is to set payload as application/json mimetype.
<set-payload value="#[payload]" mimeType="application/json" doc:name="Set Payload"/>
Add this line after the HTTP Request. Just setting the payload value as #[payload] will convert it to json While converting it to string will just print it as string and not make it Json type.

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 replace value using xpath3 expression in mule esb

<babyproducts>
<products>
<product1>
<productid>100</productid>
<productname>towel</productname>
<desc>towel</desc>
<discount>100</discount>
<validlity>10</validlity>
</product1>
<product2>
<productid>101</productid>
<productname>pillow</productname>
<desc>pillow</desc>
<discount>500</discount>
<validlity>5</validlity>
</product2>
</product>
</babyproducts>
I want to split above xml using xpath3 expression and replace the validlity value . after that i want whole set of above xml replaced values. I tried using xpath it is working fine. my requirement insists on doing using xpath3 mule expression
<splitter evaluator="xpath" expression="/babyproducts/products" doc:name="Splitter" />
<set-payload value="#[message.payload]" doc:name="Set Payload" />
<enricher source="#[flowVars.newvalidlity]" target="#[xpath('/products/validlity').text ]" doc:name="Message Enricher">
<logger level="INFO" message=" enricher done ~~~~~.." doc:name="Logger" />
</enricher>
above is my mule flow using xpath. my team wants to do using xpath3 (latest).
my expected outpu is like below with new validlity value 50 (comes from flow variable)
<babyproducts>
<products>
<product1>
<productid>100</productid>
<productname>towel</productname>
<desc>towel</desc>
<discount>100</discount>
<validlity>50</validlity>
</product1>
<product2>
<productid>101</productid>
<productname>pillow</productname>
<desc>pillow</desc>
<discount>500</discount>
<validlity>50</validlity>
</product2>
</product>
</babyproducts>
First of all your XML sample is invalid. There are unclosed tags and your xpath doesn't match the structure of the sample either.
Second of all I can only show the equivalent xpath3 expression of your xpath expression as I have already answered how to update an XML node in other questions of yours.
<splitter evaluator="xpath" expression="/babyproducts/products" doc:name="Splitter" />
Becomes:
<splitter expression="#[xpath3('/babyproducts/products/*', payload, 'NODESET')]"
doc:name="Splitter" />
And
#[xpath('/products/validlity').text]
Becomes:
#[xpath3('/products/validlity')]
No need for .text as by default returns the Stirng value. More info here: http://blogs.mulesoft.org/mule-3-6-xml-xpath-xslt-xquery3/

How to perform string operation on JSON MEL in mule esb

I want to perform some string operation on MEL I have following expression in MEL
<logger message="#[json:xy/PID/xy.3/AC]" level="INFO" doc:name="Logger"/>
OUTPUT IS
19901026000000
I want to extract 1st 4 digit then 6,7 digit .
How can I do this ??
Thanks
What about trying it in two steps?
<set-variable variableName="result" value="#[json:ADT_A01/PID/PID.3/CX.1]" />
<set-variable variableName="result" value="#[result.substring(0,4)]#[result.substring(5,7)]" />
As noted in the comments in #Ryan Hoegg answer, the JSON expression evaluator has been deprecated since Mule 3.3 and hence the best way to do this would be to use a json to object transformer
<json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.HashMap"/>
and then use conventional MEL to traverse the Map
JsonPath expression are depreciated for now and you will even not get enough document on it for doing ..
So, currently you need to use either :- <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
or <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />
or even <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object" /> to extract data from JSON depending on the JSON data