Dataweave1 ( Mule 3 ) how do I use regular expression defined in properties file? [duplicate] - mule

Is there a way in dataweave in mule 3.9 to match against a regex stored in a variable?
The simplest example would be:
<!-- does not work -->
<dw:transform-message doc:name="Get value">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload matches flowVars.regex
]]></dw:set-payload>
</dw:transform-message>
But that could be resolved using MEL #[payload.matches(flowVars.regex)] or simply
#[regex(flowVars.regex)]
A more elaborate example would be, there is a table with the following structure:
regex | value
--------------------
^typeA$ | valuefor-A
^typeB$ | valuefor-B
Then we query this table and store it in a variable:
-- not real mule code
select regex, value
from tablewithregexdefinitions
into flowVars.RegexDefinitions
And then, we try and obtain the applying entries where the regex matches a payload field:
<dw:transform-message doc:name="Get value">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
(flowVars.RegexDefinitions filter (payload.field matches $.regex))[0].value default "fallback-value"
]]></dw:set-payload>
</dw:transform-message>

Hi there is no way to do that in DW 1.0 as when using the matches with a string transforms the string into a literal regex so it is not going to be interpreted. The only solution is to create a global mel function and call it from the DW code.

Related

DataWeave 2.0 Backslash escaping

How can you get a single backslash in DataWeave 2.0?
%dw 2.0
output application/json
---
{
"attempt1": "\String",
"attempt2": "\\String"
}
Returns:
{
"attempt1": "\\String",
"attempt2": "\\String"
}
Looks like your output is a json and in json the \ always need to be escaped inside a string. That is why you are always going to see two \

DataWeave matching on dynamic regex in Mulesoft 3.9

Is there a way in dataweave in mule 3.9 to match against a regex stored in a variable?
The simplest example would be:
<!-- does not work -->
<dw:transform-message doc:name="Get value">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload matches flowVars.regex
]]></dw:set-payload>
</dw:transform-message>
But that could be resolved using MEL #[payload.matches(flowVars.regex)] or simply
#[regex(flowVars.regex)]
A more elaborate example would be, there is a table with the following structure:
regex | value
--------------------
^typeA$ | valuefor-A
^typeB$ | valuefor-B
Then we query this table and store it in a variable:
-- not real mule code
select regex, value
from tablewithregexdefinitions
into flowVars.RegexDefinitions
And then, we try and obtain the applying entries where the regex matches a payload field:
<dw:transform-message doc:name="Get value">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
(flowVars.RegexDefinitions filter (payload.field matches $.regex))[0].value default "fallback-value"
]]></dw:set-payload>
</dw:transform-message>
Hi there is no way to do that in DW 1.0 as when using the matches with a string transforms the string into a literal regex so it is not going to be interpreted. The only solution is to create a global mel function and call it from the DW code.

How to convert a string to camel case in Mule 4

Is there any function to convert a string ex: "iamhuman" to camel case "iAmHuman" in Mule 4 application.
There is a camelize function you can use in dataweave. BUT it will NOT work with your example because it won't know where the word breaks are. If you had another separator, such as underscores or hyphens then this would work:
%dw 2.0
import * from dw::core::Strings
output application/json
---
{ "camelize" : camelize("i_am_human") }

How to use a CSV file which does not have any header in dataweave?

I have a CSV file which is not having Headers.
I want to use this CSV file, use the values of each column and transform into another CSV with headers.
Is there any way to use Column positions of CSV in dataweave to access it?
Yes, you can read a CSV with no header values.
Here is how your dataweave code could be. Note the input-payload configuration which sets the header=false on reader.
<dw:transform-message doc:name="Transform Message">
<dw:input-payload>
<dw:reader-property name="header" value="false"/>
</dw:input-payload>
<dw:set-payload><![CDATA[%dw 1.0
%input payload application/csv
%output application/csv
---
payload map {
Id: $[0],
TestCol1: $[1],
TestCol2:$[2]
}]]></dw:set-payload>
</dw:transform-message>
HTH.

Data Weave not able to escape the string inside double quotes- Mule ESB

I'm trying to convert csv to csv. My deliliter is ,, there are certain texts in input which also comes as with comma example: "Fun,Chair,tables" but it will be wrapped with quotes. Data Weave thinks this is as delimiter and separates in to different column in response.
Please find the Input CSV file
sequence,items
1,Fun
2,"Fun,chairs,tables"
3,Games
But getting response as
Number,itemDetails
1,Fun
2,Fun\,Chair\,tables
3,Games
In the above response, second two is splitted which i'm not expecting
my dataweaver
%dw 1.0
%output application/csv header=true
---
payload map {
Number:$.sequence,
itemDetails:$.items
Expected response is
Number,itemDetails
1,Fun
2,"Fun,chairs,tables"
3,Games
Problem here is "Fun,chairs,tables", double quotes should go as test field, instead data weaver consider this as a delimiter.
In the Data weave header,tried with quote= " " and escape=" ". But not working.
I have tried same thing on Datamapper working perfectly fine. Believe somethings needs to be done on header, not sure what but??.
Checked the properties in read configuration properties in Data Weave CSV settings also. No help.
Edit:
With the DataWeaver escape sequence,
%output application/csv header=true escape="\""
Below is the response
Number,itemDetails
1,Fun
2,Fun",Chair",tables
3,Games
Your thoughts and suggestions will be helpful. Thanks in advance.
Did you try escape="\"" ? I think this will escape the double quotes
Try
%output application/csv quoteValues=true ,escape =" "
You can enclose the value in quotes by yourself
%dw 1.0
%output application/csv header=true
---
payload map ((item , index) -> {
Number: item.sequence,
itemDetails: item.items
} when index != 1
otherwise {
Number: item.sequence,
itemDetails: "\"" ++ item.items ++ "\""
})
Output
Number,itemDetails
1,Fun
2,"Fun\,chairs\,tables"
3,Games
we have to change the reader property like below. I've also faced the same problem while reading the csv input.
<dw:reader-property name="escape" value="""/>
above worked in my case and solved the problem