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

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.

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

Mule print BufferInputStream using payloadAs

The log message below is aimed at printing out a json error response from my api.
<logger message="#[exception.getCauseException()?.getMuleMessage()?.getPayload()]" level="INFO" doc:name="Logger"/>
The result of this log message is
org.glassfish.grizzly.utils.BufferInputStream#486bf09a
I have a requirement to pass the json response. How can I turn the above log line into something like this where it will pass the stream rather than just printing BufferInputStream.toString()...
<logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
I tried the following below that does not work.
<logger message="#[exception.getCauseException()?.getMuleMessage().payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
By the way logging a stream is not the real purpose as to why I want to parse the stream. I know that is an expensive operation. I really need to pass the stream to get some meaningful information from the response.
thanks
Yes, you are correct, you are going to have to convert the output into a string to access it. Then you will have to manipulate the output using string methods to get the desired information.

Mule getting data from ConsumerIterator

My mule flow is:
<sfdc:query config-ref="SFDC__DevInt" query="dsql:SELECT Id FROM Account WHERE Name = #[flowVars.Name]" doc:name="Salesforce"/>
<logger message="Select query: #[message.payload]" level="INFO" doc:name="Logger"/>
<foreach doc:name="For Each">
<logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
<logger message="#[message.payload.Id]" level="INFO" doc:name="Logger"/>
</foreach>
Above for loops returns the id. Instead of looping is there any direct way of getting data as iterator object? i.e I need to get the value of id.
Data inside for loops returns:
SFDC:Select query: org.mule.streaming.ConsumerIterator#2f23abf
INFO 2015-06-22 13:10:25,520
{Id=0011100000uPbqeAAC, type=Account}
At a time I will be getting single data.
I tried with #[message.payload.iterator().hasNext().next()], but getting
Message : Execution of the expression "message.payload.iterator().hasNext().next()" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: ConsumerIterator
Code : MULE_ERROR--2
The message payload after sfdc:query is an instance of org.mule.streaming.ConsumerIterator, which implements java.util.Iterator. Therefore there's no reason to call iterator() on it, it is already an iterator.
Moreover hasNext returns a boolean, not an Iterator instance, so you can't chain the calls as you did.
Your expression should thus be:
#[message.payload.hasNext() ? message.payload.next() : null]
assuming that null is the value you want in case the enumeration is empty.
You can also convert the iterator into a list if you need to use for-each
#[org.apache.commons.collections.IteratorUtils.toList(payload)]

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

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