How to convert a string to camel case in Mule 4 - mule

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") }

Related

I need help outputting 'greater than' and 'less than' characters using dataweave in xml format

i have the following dataweave code
%dw 2.0
output xml writeDeclaration=false
---
'a:b': '<'
the current output is
<a:b><</a:b>
however my desired output is
<a:b><</a:b>
is anyone has any ideas, any help is appreciated! thank you
You should not try to do that. The output is XML, and in the XML specification a lone < character must be encoded as <. Other characters are also restricted from appear as text in an XML document.
Having said that, you can generate that literal character if the element is in a CData block. You can create a CData blockin DataWeave by coercing a String to CData.
Example:
%dw 2.0
output xml writeDeclaration=false
---
'a:b': '<' as CData
Output:
<a:b><![CDATA[<]]></a:b>
If you just want to have a string output, the workaround is replace the "&lt" with "<". This is just a workaround if you don't want to use CDATA
Script
%dw 2.0
output application/java
---
write('a:b': '<',"application/xml", { "writeDeclaration": false})
replace "<" with "<"
Output:
<a:b><</a:b>

How to detect latin word in a file in mulesoft

I want to detect latin / non-english word in a file in a Mule application running in Anypoint Studio (MuleSoft products), Can anyone help me?
Basically my code fetching a file from a legacy system and read it and post the data to salesforce, while reading the file I need to detect if any latin word / non-english word are there in the name column
There is no built-in function to detect characters outside the english alphabet that I'm aware of in Mule.
One alternative is to create a custom DataWeave function and use the charCode() or charCodeAt() functions to compare the Unicode characters of each character in the file with the allowed english characters, iterating over the characters of the file. This assumes that the file is a text file that can be read as a string.
Another alternative is to implement the same algorithm in a Java class and call it using the Java Module.
This is a solution with DataWeave using a recursive function to iterate over the characters. It would be more efficient if there was a way to avoid the recursion:
%dw 2.0
output application/json
import * from dw::core::Strings
fun isEnglishChar(c)=
(c >= 65 and c <= 90) or (c >= 97 and c <= 122) or (c == 32)
fun isEnglishWord(s)=
if (sizeOf(s) > 1) isEnglishChar(charCode(s)) and isEnglishWord(s[1 to -1])
else if (sizeOf(s) == 1) isEnglishChar(charCode(s))
else true
---
payload map isEnglishWord($.name)
Input:
[
{
"name": "has space"
},
{
"name": "JustEnglish"
},
{
"name": "ñó"
}]
Output:
[
true,
true,
false
]
Using functions makes it easy to reuse and to modify the logic if needed.
use the below regex to find non-English words, created a simple example as below
input:
{
"message": "你好"
}
code:
%dw 2.0
output application/json
---
payload.message contains (/[^\x00-\x7F]+/)
output : true
one more working example screenshot is as below:
Assuming you are able to parse input and get the string value in name field. You can iterate over the String value and for each word in the string, apply the below logic on each word.
Logic: Assuming the word is either english word or non-english word, pick the first letter from the word and check if it contains in the 26 english alphabets. When English word, value of No_Latin_Word should be True else False.
%dw 2.0
output application/json
//var name = "ĥć"
var name = "hc"
---
No_Latin_Word : upper(name[0]) contains /[A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z]/

Dataweave Escape Double Quote and , while reading a json and writing to csv

am getting json and trying to bulk load to salesforce as a csv . But am getting errors when i do that .
Any idea how to escape the characters?
Input:
[
{
"Name": ".054\" X 6' , Guide \\ Wire2",
}
]
dw:
%dw 2.0
output application/csv escape="",quote=""
---
payload
Following this Bulk API v2 Salesforce Documentation, I think the Dataweave writer configuration you need is the following one:
output application/csv quote="\"",quoteValues=true,escape="\""

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 \

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