function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex.AcostaAlex.Acosta 

JSON and escaped double quote

I'm currently making a callout to a 3rd party vendor and I'm getting a huge response. What is currently messing me up is this section:

     "MessageSeq":13,"Description":"config file: config-qa1.txt\"","MessageTS":"2014-02-17T14:46:58","MessageLevel":4,"IsSystemMessage":false

As you can see here, the portion at the end of the Description value is ended with backslash double quote. If I attempt to parse this via the JSONParser I get an exception. Does anyone have an recommendations on how to resolve this?
Ashish_SFDCAshish_SFDC
Hi Alex, 


It sounds like your exception is encountered not by the response, but by Apex code mimicking it?

The response format is correct: the backslash \ is correctly placed to permit a " in the JSON.

Check it out, if I put your string, literally, as plaintext into something returnable by an Apex function:

<apex:page contentType="text/plain">{"MessageSeq":13,"Description":"config file: config-qa1.txt\"","MessageTS":"2014-02-17T14:46:58","MessageLevel":4,"IsSystemMessage":false}</apex:page>
And then obtain that string in Apex:

String data = Page.MyTestPage.getContent().toString();
Map<String,Object> obj = (Map<String,Object>)Json.deserializeUntyped(data);
System.debug(obj);
he will deserialize just fine!

But if you intend to recreate that string in Apex source code, say in a test method, as a WebServiceMock or HttpCalloutMock you must escape the backslash itself in Apex:

String data = '{"MessageSeq":13,"Description":"config file: config-qa1.txt\\"","MessageTS":"2014-02-17T14:46:58","MessageLevel":4,"IsSystemMessage":false}';
//see double backslash here_______________________________________________^

See more information in the below thread, 

http://salesforce.stackexchange.com/questions/28105/json-and-escaped-double-quote


Regards,
Ashish