0
votes

Hi like to replace backslash with an empty character.

My Payload is

{
"id" : 1234567,
"requestDate" : "2021/02/02",
"responseXML" : "<DRIVEResponse ResponseDateTime=\"2/15/2021 10:09:10 AM\" TimeZone=\"EST\">",
}

I tried some ways but it seems nothing is working am not sure what is the mistake i am doing .

payload.responseXML replace "\"  with ""

My Expected response is

<DRIVEResponse ResponseDateTime="2/15/2021 10:09:10 AM" TimeZone="EST">
2

2 Answers

1
votes

It looks like the mistake is trying to remove the backlash character (\). It is the JSON escape character that is needed to escape the double quote character inside a JSON string. Note that responseXML contains as its value a string that contains an XML value. XML uses quotes around attribute values. The only way to represent those in a JSON string is to escape. That's the expected behavior as defined by JSON standards. It should be fine unless you are processing it with a non compliant JSON parser. If at some point you need to evaluate that string as an XML, the resulting XML will not have a backlash.

For example I will use your input data an read responseXML. Note however that the XML is not well formed because it lacks a closing tag. I just added a forward slash (/) to close the tag:

%dw 2.0
output application/xml
---
read(payload.responseXML,"application/xml")

Input payload:

{
"id" : 1234567,
"requestDate" : "2021/02/02",
"responseXML" : "<DRIVEResponse ResponseDateTime=\"2/15/2021 10:09:10 AM\" TimeZone=\"EST\" />"
}

Output:

<?xml version='1.0' encoding='UTF-8'?>
<DRIVEResponse ResponseDateTime="2/15/2021 10:09:10 AM" TimeZone="EST"/>

There is actually an alternative. If you can control the generation of the XML you could use single quotes. The XML standard allows for both single quotes or double quotes. I don't recommend to do this though. It looks like it is for hiding an issue in some other component. It feels like a bad practice to me.

1
votes

That is how your " are escaped within a string that is surrounded by "".

\" is rendered as " in the eventual string. See the screengrab below and notice how \" does not get replaced while " does get replaced in the sentence.

enter image description here