DataWeave 2.0 Backslash escaping - mule

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 \

Related

How to solve parse issues when a CSV has a field content escaped with double quotes

The input is received from a Salesforce Bulk API query.
INPUT
"RecordTypeId","Name","Description"
"AAA","Talent 2022 - Skills Renewal - ABC","DF - 14/03 - Monty affirmed that the ""mastercard approach"" would best fit in this situation. I will connect (abc, def, ghi) and the confirm booking tomorrow (15/03)"
SCRIPT:
%dw 2.0
output application/csv separator=",", ignoreEmptyLine=false, quoteValues=true, quoteHeader=true, lineSeparator="\r\n"
---
payload
OUTPUT:
"RecordTypeId","Name","Description"
"AAA","Talent 2022 - Skills Renewal - ABC","DF - 14/03 - Monty affirmed that the , def, ghi) and the confirm booking tomorrow (15/03)"
Expected OUTPUT:
The column description has " and , in it and therefore some description content is getting lost and some is getting shifted to different columns. I need entire description value in one column
The escape character has to be set to a double quote (") for DataWeave to recognize that "" is an escaped quote and not the end of a string. You can not use replace or any string operation because they are executed after the input is parsed.
You need to configure the reader properties in the source of that payload. For example in the SFTP or HTTP listeners, or whatever connector or operation reads the CSV. There you can add the outputMimeType attribute and set the input type and its properties. Note that because the flow is in an XML file you need to be mindful of XML escaping also to use double quotes, and also need to escape the double quotes as DataWeave expects it, with a backslash (\).
Example:
outputMimeType="application/csv; escape="\"""
It looks like your payload is using " as escape character. By default DataWeave expects \ as the escape character for CSV, so you will need to specify the escape character explicitly while reading your input, after which DataWeave should be able to read the completely description as a single value.
For example the below DataWeave shows how you can use the input derivative to read your csv correctly. I do not know what exactly is your expected output so I am just giving an example that writes the value of description as text
%dw 2.0
input payload application/csv escape='"'
output text
---
payload[0].Description
The output of this will is
DF - 14/03 - Monty affirmed that the "mastercard approach" would best fit in this situation. I will connect (abc, def, ghi) and the confirm booking tomorrow (15/03)

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>

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="\""

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

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