In below dataweave code, comparision is not working properly for me.
Properties file key and value as below :
domain=Sales Domain, Retail Domain
Dataweave code :
%dw 2.0
output application/json
var test = 'Sales Domain'
---
{
result: if(upper(test) != (upper(Mule::p('domain')))) "Not equal" else ("equal")
}
Could you please help on this ?
The property domain contains the string Sales Domain, Retail Domain. The comparison with the variable test which contain the string Sales Domain. Obviously both strings are different, even converting to uppercase (upper()).
If you want to check if one string contains the other you could use the contains() function instead.
In DataWeave there is not a not-equal operator to compare the values. You can use the not operator to negate the result of an expression.
For example:
%dw 2.0
output application/json
var test = 'Sales Domain'
---
{
result: if(upper(Mule::p('domain')) contains upper(test)) "contains" else "not contains"
}
Related
I'm new to migrating the mule 3 apps to mule 4 I have done almost conversion but one expression stopped my flow and not able to achieve the logic for it if anyone has an idea regarding the expression to transform please help me
Expression:
if(flowVars.maindata.keySet().contains(payload.idCaseNumber))
{
flowVars.temporary=[];
flowVars.maindata.get(payload.idCaseNumber).add(map);
}
else
{
flowVars.temporary.add(previousdata);
vars.maindata.put(payload.idCaseNumber,temporary);
}
I have tried up to my knowledge on the above code but still I'm getting problem
flowVars.maindata.get(payload.idCaseNumber).add(map);
In Mule 3 the expression language is MEL. In Mule 4 it is DataWeave 2.0. You can't just translate directly. MEL is an imperative scripting language, similar to a subset of Java and it is easy to call Java methods. DataWeave 2.0 is a functional language. Furthermore Mule 4 operations (example: a , , etc) can only return one value, which can be assigned to the payload or to one variable.
For your snippet I'll assume that maindata is a map. You can use two set-variable to assign each variable:
<set-variable variableName="temporary" value="#[ if( namesOf(vars.maindata) contains payload.idCaseNumber ) [] else vars.temporary ++ **previousdata** ]" />
I don't know exactly what do you use for previousdata.
To update the variable maindata it is probably a good match for the update operator, in a separate or Transform operation, with the same condition than for vars.temporary.
Update:
I'll assume vars.maindata is a map, which DataWeave will consider an object, and each element is a list. As an example of doing an 'upsert' operation with a dynamic selector:
%dw 2.0
output application/java
var temporary=[5]
var maindata={ a:[1,2,3,4] }
var myKey="a"
---
maindata update {
case data at ."$(myKey)"! -> if (data != null) data ++ temporary else temporary
}
You could replace in above script the DataWeave var temporary with the expression from my example above, and the other DataWeave variables with the Mule variables (vars.name) or payload. If you change in above example myKey to have value "b" you will see that key being added.
I'm trying to extract a part of a string using a regular expression:
\[CODE\] \((.*?)\)
Given the string [CODE] (ABC-212) Rest of the title this match (https://regexr.com/557qd)
I used this regexp in my current java application, and now I'm trying to transform this on a Mule App.
Reading the documentation I see that I need to use a Transform, so I setup like this:
%dw 2.0
output application/json
---
vars.subject match(/\[CODE\] \((.*?)\)/)
I stored the string on the var called subject.
Using that regexp on Mule, do not works.. but in my java application does. What I'm doing wrong ?
DataWeave doesn't seem to have a global search flag (the g at the end of the original regex). This means the regular expression has to match the entire string. You use the parenthesis to indicate capture groups that will be returned additionally to the matched string.
Script:
vars.subject match(/^\[CODE\] \((.*)\).*/)
Output:
[
"[CODE] (ABC-212) Rest of the title",
"ABC-212"
]
Match in DataWeave returns an array that contains the entire matching expression, followed by all of the capture groups that match the provided regex. And in your case "Rest of the title" was not matching as per per Regex provided by you.
%dw 2.0
output application/json
var subject="[CODE] (ABC-212) Rest of the title"
---
//Catch here is, Regex should be fully matching as per input,
{
matchesOnly:subject matches /(\[CODE\]) \((.*?)\)[\w\W]*/,
matchOnly: subject match /(\[CODE\]) \((.*?)\)[\w\W]*/
}
I am using Anypoint Studio 7.3 and Mule 4.1.
I am looking to dynamically pass the field name from a JSON payload when transforming a message so on the 1st call I want to use the values in the "cat_name" field and when I call it a 2nd time I want to use the values in the "dog_name" field as the output message structure will be the same. So for "cat_name" I would want $.(vars.codetest) to be resolved as payload.cat_name and for "dog_name" I would want $.(vars.codetest) to be resolved as payload.dog_name
Is there a way of doing this?
%dw 2.0
output application/json
---
(payload distinctBy $.#[vars.codetest]) map ((payload01, indexOfPayload) ->{
name: $.(vars.codetest)
})
Thanks for any help
Something like this should work:
%dw 2.0
output application/json
---
payload
distinctBy $[vars.codetest]
map ((element) -> { name: element[vars.codetest] })
You might need parens around codeTest (i.e. (vars.codetest)) so that it gets evaluated before the lookup.
In Mule4, I need to convert json sample data in to dynamic XML format, I have tried with dataweave(2.0) field mapping, getting null values. Does anyone can help me on this?
If the question is just asking out to build up XML output from JSON input, that is a pretty open-ended question. What do you want to evaluate dynamically? You could, for example, use part of the payload to set values in the DataWeave expression.
There is a more difficult version of this question: how to dynamically evaluate DataWeave code constructed into an input string, where this string could be read from various script files, or even constructed in-line from some input data (payload, attributes, or variables).
Here is another example covered in our MuleSoft DataWeave training course at http://training.mulesoft.com.
You can use a Dynamic Evaluate component to dynamically evaluate a constructed DataWeave expression string. Here is an example that replaces the uName parameter with a dynamic value.
Also, the expression is configured to read in different script files based on some condition:
output application/json
---
do {
var choice = attributes.queryParams.script default "NO_SCRIPT"
---
if(choice == "NO_SCRIPT")
"output application/json --- {result: 'NO SCRIPT ENTERED'}"
else if(choice == "script1") vars.script1
else if (choice == "script2") vars.script2
else read(choice)
}
Here are two example scripts that substitutes values for uName and produce different types of output (XML vs. JSON).
This is script1:
output application/xml
---
root: { message: "order "
++ attributes.queryParams.orderid
++ " has been received from "
++ uName, items: payload.items}
This is script2:
output application/json
---
root: { message: "Order2 "
++ attributes.queryParams.orderid
++ " has been received from "
++ uName, items: payload.items}
Notice that this example is dangerous. It lets the web client inject any DataWeave code into the Mule flow, so this example should never be copied into production code, but it does demonstrate the ability to run any DataWeave code passed into a Mule application.
I have 2 field in the input, one is primary_language & the other is secondary_language. I have a case where I have to lookup values present in these two fields and then return a specific value according to the table.
For example
If primary language is English & secondary language is null then English will be the output & if primary language is Spanish & secondary is Sign then put None in the output & so on. Can one tell how we can perform this in dataweave in mulesoft.
Do you really have some more dynamic logic or its just the two conditions you mentioned above?
you can use when/otherwise or call another flow to get the value.
%dw 1.0
%output application/java
---
{
language: 'English' when (payload.primary == 'English' and payload.secondary is :null)
otherwise ('None' when payload.primary == 'Spanish' and payload.secondary == 'Sign'
otherwise ''
),
language2: lookup("testFlow",payload)
}
I would recommend to create another flow which performs this lookup for you (potentially you could do a database call, or something else like a groovy script), and store your values and what you expect to get returned based on those values.
https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation#expressions-that-call-external-flows covers this concept a little bit, but the general idea is the following:
language: lookup("myLookupFlow", payload)
Then, all you need to do is query your dataset based on primary and secondary, and you will get your "transformed" value back.