Mulesoft MEL Expression Get String Value Of Payload Data Type - mule

I want to extract my payload's class name in a MUnit assert so I can verify the payload is always of the correct type. I've tried 2 MEL expressions, but both return null in the MEL expression evaluator. The funny thing is that if I remove the .name part of the expression then I see a key called "name" with the value that I need. Any ideas?
payload.class.name
message.dataType.type.name

One way is: #[payload.getClass().getSimpleName()]

Related

Getting error "unbalanced braces" in data weave in mule 3.9.3

I am using choice router to evalute expression. Here is the expression
and I tested this expression in dataweave here is the result.
but when I use the choice router to evalute the expression I am getting this result
and another thing the value of "payload.relations.rel" is "Microsoft.VSTS.Common.TestedBy-Reverse" why I am getting false for this expression
bench : payload.relations.rel == "Microsoft.VSTS.Common.TestedBy-Reverse"
The error is not from DataWeave. In Mule 3.x the expression language used is MEL (Mule Expression Language). DataWeave is only used in Transform components. This is different from Mule 4.x where DataWeave 2 is used as the expression language. Testing the expression of the choice in DataWeave is not a good test.
Also your tests show that you are comparing an array (payload.relations.rel) to a String. Try fixing the comparison first. Then if you still have the error try putting it into a logger component before the choice and see if it prints the right result.

Unable to understand Mule 3 expressions

I know Mule 4 but came across Mule 3 application and trying to understand some expressions and connectors. Can somebody explain.
Set Variable expression 1:
#[dw("p(flowVars.someKey)")]
Set Variable expression 2: (input XML payload)
#[xpath3('local-name(//*:Body/*[1])')]
What does DOM to XML do?
I'll assume these are 3 questions:
What does this expression do?
#[dw("p(flowVars.someKey)")]
This is a MEL expression (MEL is the expression language in Mule 3) which executes a dynamic DataWeave expression, to obtain a configuration property, where the name of the configuration property is dynamically obtained from the value of flow variable someKey.
What does this expression do?
#[xpath3('local-name(//:Body/1)')]
This MEL expression call the xpath3() function to evaluate an XPath 3.0 expression on the payload.
What does DOM to XML do?
This question has been answered previously.

what are the various ways to fetch the properties from payload?

What is the difference between reading the properties from payload. for example there is a property in the payload which is named as con_id. when i read this property like this #[payload.con_id] then it is coming as null. where as #[payload.'con_id'] is returning the value.
few other notations which i know of is #[payload['con_id']] or #[json:con_id]
which one should be used at which scenario? if there are any special cases to use any specific notation then please let me know the scenario also.
Also, what is the common notation that has to be used from a mule soft platform supported point of view.
In Mule 3 any of those syntax are valid. Except the json: evaluator is for querying json documents where as the others are for querying maps/objects. Also the json: evaluator is deprecated in Mule 3 in favor of transforming to a map and using the MEL expressions below.
payload.property
payload.'property'
payload['property']
The reason the first fails in your case, is beacaue of the special character '_'. The underscore forces the field name to be wrapped in quotes.
Typically the . notation is preferred over the [''] as its shorter for accessing map fields. And then simply wrap property names in '' for any fields with special chars.
Note in Mule 4, you don't need to transform to a map/object first. Dataweave expression replace MEL as the expression language and allow you to directly query json or any type of payload without transforming to a map first.

Access FlowVar dynamically in DataWeave

I'm trying to access a FlowVar name dynamically in DataWeave.
For example:
I have a flowVars named taxInfo123. This is a linked list and my applicant.ApplicantID = 123
In my dataweave, I want to access this dynamically. Something like the following:
"TaxInfo": flowVars.'taxInfo'+applicant.ApplicantID map ((taxIdentificationDetail , indexOfTaxIdentificationDetail) -> {
This obviously doesn't work, and I'm hoping this is possible and I just need the correct syntax.
If you need to dynamically create the variable name, you can use the flowVars[key] syntax instead of the flowVars.key syntax. In your scenario:
"TaxInfo": flowVars[('taxInfo' ++ (flowVars.applicant.ApplicantID as :string))]
I assumed applicant was also a flowVar but you could just as easily use payload.applicant.ApplicantID or whatever your situation calls for. I also assumed it was a number so I had to cast it as a string.
When you use this syntax you want to make sure you wrap the key expression in parenthesis so it is evaluated first and then the flowVar is resolved.
So to summarize:
If you know the variable name is 'taxInfo123' -
flowVars.taxInfo123 or flowVars[taxInfo123] are both valid
If you need to create the variable name dynamically -
flowVars[(expression)]
Hope that helps!
Forming the variable name needs append operator like ++. Please go through the MuleSoft documentation for Dataweave operators to get better understanding of how much flexiblity is possible in Dataweave.
https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators

mule: how to add value to payload array list

I have a payload as list of maps and another flow variable as map.
I wanted to add flow variable to payload list. I tried using this expression #[payload.addAll(flowVars['entitlement'])]in expression component. But it sets the payload to boolean value true.
Use expression component like
<expression-component doc:name="Expression"><![CDATA[payload.addAll(flowVars['entitlement'])]]></expression-component>
Hope this helps.
Try using Set Payload transformer.
and add
[payload.addAll(flowVars['entitlement']) as value.
You set the payload as the value returned by addAll(). It's like doing payload = payload.addAll(flowVars['entitlement']) in Java addAll() returns a boolean, that's why your payload becomes true.
You can use instead:
#[payload.addAll(flowVars['entitlement']); payload)
This will perform your addAll() operation on your payload and then return this modified payload afterward. ; allow your to perform multiple expressions in MEL
You can use dataweave transform component and add the elements in various ways like using the ++ operator or use map operator to change the structure as per your requirements.