How to perform string operation on JSON MEL in mule esb - mule

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

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 I do check if the variable exist in Mule 3.2?

How do check if the variable exist in Mule 3.2?
How create or replace a variable?
My Flow Exception is
<choice doc:name="Choice">
<when expression="#[myVar==null]">
<processor-chain>
<set-variable variableName="myVar" value="value1" doc:name="myVar"/>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<set-variable variableName="myVar" value="#[variable:myVar]" doc:name="myVar"/>
</processor-chain>
</otherwise>
</choice>
"myVar==null" does not work
if the variable is an invocation variable please try
flowVars['myVar'] == null
Worst case scenario you could do:
message.getProperty('myVar', org.mule.api.transport.PropertyScope.INVOCATION).
Please notice the scope is an enum so you should provide the canonical in a MEL expression.
HTH
You can use this MEL to print in log or use in Choice component
#[message.invocationProperties('myVar')] == null
There is a component "Message Property" which will help you remove or replace or create Mule message properties. You should use that to perform your operation.
To best find it use keyword "propert" in the search of pallets in the Mule studio and explore the various components there to suit your needs.

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/

Aggregate data from for-each loop

Scenario - Converting a csv file to json format, taking each json element and making a get request api call. I am doing this in a for-each loop sequence. I am getting a json response (extracting eventId and cost from each). Now I wish to club all these responses together under the main header listings and make a bigger json payload.
For example:
{
"listings": [
{
"eventId":"8993478",
"cost":34
},
{
"eventId":"xxxxxyyyy",
"cost":zz
},
]
}
How would I do this for all iteration entries. I can do it for a single entry(using groovy script).
You could define a variable before the for-each loop as an empty list with:
<set-variable variableName="listings" value="#[[]]" />
Then, on each iteration inside the for-each loop add an element to the previous variable with:
<expression-transformer expression="#[flowVars.listings.add(flowVars.iterationMap)]" />
In the previous code fragment I used the variable flowVars.iterationMap to denote the map generated on each iteration.
Finally, if needed, you can add a set-payload transformer after the for-each loop:
<set-payload value="#[flowVars.listings]" />
HTH, Marcos
You can use the Batch module but you would have to rewrite this logic a little bit different. For example, you will no longer be able to use an aggregation flowVar like Marcos suggested. Instead, you would need to use a fixed size batch:commit block (which would actually be better in many ways, for example you could start sending bulks to the remote API while still processing some of the other records in the background).
...I like Marco's answer and it worked perfectly for my use case.
Simply creating an array in a flow variable and using the add() method on the array in a ForEach scope did the trick.
The OP follow up question was a good one. It prompted me to do an alternate test using the approach suggested. See both of my flows here:
<flow name="sampleAggregatorFlow" doc:description="this is a simple demo that shows how to aggregate results into an accumulator array">
<http:listener config-ref="manage-s3-api-httpListenerConfig" path="/aggregate" allowedMethods="GET" doc:name="HTTP"/>
<set-payload value="#[['red','blue','gold']]" doc:name="Set Payload"/>
<set-variable variableName="accumulator" value="#[[]]" doc:name="accumulator"/>
<foreach doc:name="For Each">
<expression-transformer expression="#[flowVars.accumulator.add(payload)]" doc:name="addEm"/>
</foreach>
<set-payload value="#[flowVars.accumulator]" doc:name="Set Payload"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>
<flow name="Copy_of_sampleAggregatorFlow" doc:description="this is a simple demo that shows how to aggregate results into an accumulator array">
<http:listener config-ref="manage-s3-api-httpListenerConfig" path="/aggregate2" allowedMethods="GET" doc:name="Copy_of_HTTP"/>
<set-payload value="#[['red','blue','gold']]" doc:name="Copy_of_Set Payload"/>
<set-variable variableName="accumulator" value="#[new java.util.ArrayList()]" doc:name="Copy_of_accumulator"/>
<foreach doc:name="Copy_of_For Each">
<expression-transformer expression="#[flowVars.accumulator.add(payload)]" doc:name="Copy_of_addEm"/>
</foreach>
<set-payload value="#[flowVars.accumulator]" doc:name="Copy_of_Set Payload"/>
<json:object-to-json-transformer doc:name="Copy_of_Object to JSON"/>
</flow>
Both flows produced the same outcome:
[
"red",
"blue",
"gold"
]
Tests conducted 12/26/2017 with Anypoint Studio 6.4.1 and wth Mule Runtime 3.9

SMTP subject expression evaluation fails

I have a flow where I want to evealuate an expression on the smtp subject attribute but always get mvel parse expression though the logger give me the right values.
<json:object-to-json-transformer doc:name="Object to JSON"/>
<logger message="MYRequestPayloadID #[json:RequestPayloadID] #[json:ResponseStatusCd]" level="DEBUG" doc:name="Logger"/>
<smtp:outbound-endpoint host="${mail.host}" to="${mail.to}" from="${mail.from}" subject="Error Response for PayloadID #[json:RequestPayloadID], Status #[json:ResponseStatusCd]" responseTimeout="10000" doc:name="SMTP />
I am not sure why in the subject #[json:RequestPayloadID] and #[json:ResponseStatusCd] evaluation fail though I get the data back in the logger . Thanks.
Expressions are not supported everywhere in Mule (alas): this is one place where it isn't.
You have to use message properties to set the subject dynamically:
<set-property propertyName="subject"
value="Error Response for PayloadID #[json:RequestPayloadID], Status #[json:ResponseStatusCd]" />
FTR json: is the old expression style, nowadays you should be using MEL instead: http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+Tips#MuleExpressionLanguageTips-JSONProcessing