Mule how to map Json values from payload to flow variables - mule

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

Related

how to use mel expression on salesforce result to extract single value

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

Converting CSV to array using DataWeave

I'm using Mule Requester to load a CSV file. After the file is loaded, the payload is a byte array, which I store in a flow variable, mycsv. I keep getting an exception.
org.mule.api.MessagingException: Exception while executing:
NetIds: flowVars.mycsv map $."My Column Name"
^
Cannot coerce a :binary to a :array (com.mulesoft.weave.mule.exception.WeaveExecutionException). Message payload is of type: byte[]
Here's my DataWeave code:
%dw 1.0
%output application/java
---
{
Values: flowVars.mycsv map $."My Column Name"
}
The previous flow element is a choice, so I set the metadata on that to output to a FlowVar with the right name and referenced a sample CSV file, so DataWeave things the variable type is List<Csv>.
How can I read the CSV? Thanks!
It's because it doesnt know the mimeType of the flowVar because it's not set. Try this before the dataweave transformer:
<set-variable value="#[flowVars.mycsv]" variableName="mycsv" mimeType="application/csv" doc:name="Variable" />
or set the mimeType when you first read the csv.
This is the first time I found a requirement to put two different formats into one csv file. In order to get the expected result, I prefer to use this trick:
Create both expressions and combine them into one simple array, using flatten
Add an empty object as line separator

How to refer payload which is a ResultSet Iterator in mule dataweave?

Sql query returns a streamed output as Resultset iterator object from the Database component.
i want to convert this to xml in dataweave. But don't know how to refer the incoming object,
If it's a map i can access it simply by using .operator like payload.student
Tried using payload.next() but it gives an error. Also tried the following,
%var input1 payload as :iterator but still wont' work
Here the steps:
Drag and drop the Transform Message (Dataweave) component after your configured DB Connector. You will see that the input payload for dataweave script is filled with the db result List<Map>.
Then you can access the fields, using the map function in dw.
dw script
%dw 1.0
%output application/xml
---
{
"Results":{
(payload map {
"key1":$."db_field1",
"key2":$."db_field2"
})
}
}
Can you post your code (XML) and a screenshot of dataweave or debugger?
My first guess would be to use a standard transfomer to transform that object to a list or map before the dataweave transformer.

LDAP Search json output

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.

mule able to modify json output datamapper

I'm trying convert to csv to json format using mule datamapper ,it is working fine.
below output it produce
[ {
"propertyState" : "AL",
"propertyCity" : "NJ",
"propertyZipCode" : "67890",
"propertyCounty" : "US"
} ]
But want to remove [ ] this from json format. using datamapper is it possible
mule modify json output datamapper
[] defines List for the elements .. That is required if your elements are repeating and a Valid JSON format ..
If you don't want the [] to be there then the work around will be use <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to List" /> to extract each elements value from the JSON payload and create your payload using expression ..
But again this is not a recommended approach since you always require [] in your JSON as you will be getting multiple rows from CSV file and the JSON format should have [] since that will represent it as List which is a valid format