In my mule flow I have an LDAP Paged Result Search as below. I want to get the output in json. I am getting back an ArrayList.
<flow name="ldapsearchFlow1" doc:name="ldapsearchFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="3555" doc:name="HTTP"/>
<ldap:paged-result-search config-ref="LDAP" baseDn="${xxx.ad.base}" filter="(objectClass=*)" pageSize="100" doc:name="LDAP">
<ldap:attributes>
<ldap:attribute>uid</ldap:attribute>
<ldap:attribute>cn</ldap:attribute>
</ldap:attributes>
</ldap:paged-result-search>
<ldap:ldap-entry-to-ldif doc:name="ldap1"/>
<json:object-to-json-transformer doc:name="Object to JSON" />
</flow>
Output is as below. I also dont need count , attribute count etc.
���sr�java.util.ArrayListx����a��I�sizexp��%w��%t�W{"dn":"CN=aaronj,ou=people,dc=xxx,dc=org","attributes":{"count":1},"attributeCount":1}t�W{"dn":"CN=abbasa,ou=people,dc=xxx,dc=org","attributes":{"count":1},"attributeCount":1}t�X{"dn":"CN=abbottl,ou=people,dc=xxx,dc=org","attributes":{"count":1},"attributeCount":1}t�\{"dn":"CN=abeyrathnep,ou=people,dc=xxx,dc=org","attributes":{"count":1},"attributeCount":1}t�Z
There are two issues in your config related to the usage of ldap-entry-to-ldif:
it converts a single LDAP entry to LDIF, but you use it to process a collection of results from ldap:paged-result-search. You need to either pick one LDAP entry from the search result (with a MEL expression transformer) or use ldap:lookup to return a single LDAP entry.
it converts an LDAP entry its LDIF representation, which is a String. You then serialize this String as JSON. I think you need to pick one, either replace ldap-entry-to-ldif with ldap-entry-to-map so it can properly be serialized to JSON or remove the json:object-to-json-transformer and let your flow return the LDIF representation. Because you stated that you want to remove some values from the final representation, I feel that you're more interested in the JSON representation than the LDIF one. In that case the former approach is what to do: after the ldap-entry-to-map, you could then use a MEL expression transformer to filter the keeps you don't want, prior to JSON serialize the map.
Related
Can You please help me to transform the data from database?
I mean the transformation is defined in Database.
I am retrieving the transformation from Database and put it in Datawave(DW).
But it is not working. It is showing the output as string.
Following i am retrieving from database:
flowVars.orderJsonData map
{
phone: flowVars.orderJsonData[0].phone
}
I put this into variable and put into the datawave as output
But it is working as string only.
Please help.
Assuming flow variable dwexp has the dataweave expression, you can execute that using expression-transformer. Below is the syntax
<expression-transformer expression="#[dw(flowVars.dwexp,"application/json")]" doc:name="Expression"/>
dw() function is in the format
#[dw("<Dataweave script>","Output Mime type")]
I'm new to salesforce using mule. I want to retrieve value and assign it to a flowVar from the result set returned by the Salesforce query. I tried using payload.HeadCount, payload['HeadCount'] and payload[0].HeadCount none of them are worked.
Any help would be appreciated.
#Boss,
Salesforce returns org.mule.streaming.ConsumerIterator as a payload which in turn implements java.util.iterator, so, either you can use next() operation followed by the field name(in your case HeadCount) or convert the payload into java.util.ArrayList and set it as payload using #[org.apache.commons.collections.IteratorUtils.toList(payload)] and then iterate the payload same as java.
In short, you can try with the below MEL
#[payload.hasNext() ? payload.next().Headcount: "something else"]
I have JDBC where I'm calling the stored Procedure, It is returning the response as below, But I'm pretty not sure how to extract the value of result set
Please find the response from DB
{updateCount1=4,resultSet1=[{XML_F5RYI-11YTR=<Customers><Customer1>John<Customer1><Customer2>Ganesh<Customer2><Customers>}],resultSet2[{SequenceNumber=94}],updateCount2=1, updateCount3=4}
I have used the this expression #[message.payload.get(0)], It has return the ResultSet as below, But not exactly value required. I need to take the xml value of XML_F5RYI-11YTR.
{XML_F5RYI-11YTR=<Customers><Customer1>John<Customer1><Customer2>Ganesh<Customer2><Customers>}
Also tried like below
#[message.payload.get(0).XML_F5RYI-11YTR] but getting error , not able to extract the xml.
Could you please suggest how can I extract the xml from the ResultSet1
In most cases, the way you did it should work. I think what is happening here is that the hyphen in the column name is interpreted by the MEL parser as a subtraction. So you could change yours to this syntax, and it should work:
#[message.payload.get(0)['XML_F5RYI-11YTR']]
Also you can omit "message", as payload is resolvable directly:
#[payload.get(0)['XML_F5RYI-11YTR']]
You could use array bracket syntax to access the first row in the result set, instead of the get method:
#[payload[0]['XML_F5RYI-11YTR']]
Finally, you might want to do something for each row returned from the database. If you use a collection-splitter or a for-each, your payload will be the map that represents the row, instead of a list of maps representing the whole result set:
<collection-splitter />
<logger message="#[payload['XML_F5RYI-11YTR']]" />
EDIT
To access the result set in the payload shown in the question, you would need to access it like so:
#[payload.resultSet1[0]['XML_F5RYI-11YTR']]
The database connector gives you a list of maps. The map keys will be the name of the columns. Therefore if you want to get updateCount1, you can use something like this:
#[payload.get('updateCount1')]"
Thump rule - you database connector gives you list of map, not sure what format does it is carry, if you want XML_F5RYI.. value then do the below
[message.payload.get(0)] convert it to json or map from which #[message.payload.get("XML_F5RYI-11YTR")]
I have a JDBC COnnector which retrieves values from the database and I have mapped them from object to JSON. Now I want to extract specific values from the json to flow variables. When I try to log #[message.payload], I get the full payload in the log which is in JSON format. However when I try to choose an attribute (eg. testattribute in json) #[message.payload.testattribute], I get mule expression error. How do I refer to the json values?
Once the payload is a JSON string, you can't extract anything from it anymore with an expression.
So either:
use MEL to extract the values into flow variables prior to convert the payload into JSON,
transform the JSON payload to either POJOs or Map/Lists, use MEL to extract the values into flow variables, and re-transform the payload back to JSON.
If your requirement is to get the values from json data you can use a simple MEL expression like
#[json:<name of the node>]
For example if you have an sample json data like this
{
"token" : 123,
"id" : 456,
"email" : "abc#abc.com",
"status" : "Success"
}
and if you want get the id from the data just use the below simple MEL.
#[json:'id']
Mule has JSON-to-Object transformer which can be used to get JSON elements.
For example if your JSON is following :-
{
"token" : 123,
"id" : 456,
"email" : "abc#abc.com",
"status" : "Success"
}
Now, to extract the elements, you need to use :-
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
And then can extract like :- #[message.payload.email] or #[message.payload.status]
Use components as follow
Object - String
Object - JSON
then u can directly extract using value using json expression as #{json:email},
in below xml i have used the variable component to set the #{json:email} in "var1" flow variable for usability
Note: json expression is even available in studio version of 5.x
<object-to-string-transformer doc:name="Object to String"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<set-variable variableName="var1" value="#[json:email]" doc:name="Variable"/>
Can someone please let me know how to concatenate multiple values in mule?
Something like,
#[payload.getPayload()].concat(#[getSubject()])
I assume you are using Mule 3.3.x or above. If so you can use Mule Expression Language(MEL).
One example using MEL is:
#['Hello' + 'World']
Or MEL also allows you to use standard Java method invocation:
#[message.payload.concat(' Another String')]
Cheat sheet on MEL
MULE 4 Update
For Mule 4. Dataweave 2.0 is the main expression language:
Simple concat:
#['Hello' ++ ' World']
Other alternative is to use Mule Design plugin :
Drop an "Append String" operation as many times as you need.
This operation takes the message payload of the previous step and concats a specified string to it.
Not sure about performance details, but it will be surely more easy to maintain.
Append to String - MuleSoft
you can declare a string buffer using expression component
<expression-component doc:name="Expression"><![CDATA[StringBuffer sb = new
StringBuffer();
flowVars.stBuffer=sb;
]]></expression-component>
and then append use append on string buffer any where in the flow.
flowVars.stBuffer.append("string to append")
Once done use #[flowVars.stBuffer] to access the concatenated string
If you want to add two different values received through payload in the mule flow then we can use concat() method.
For example below we have received values through arraylist where i am adding two diffrent fields i.e. FirstName and the LastName -
concat(#[payload[0].'firstname']," " #[payload[0].'lastname']