Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

Exception Message:

System.JSONException: Unexpected character ('"' (code 34)): was expecting comma to separate OBJECT entries at [line:1, column:3699]

Updated with sample Code

public class ResponseWrapper{
    // fields from JSON to auto map
    public Integer messageSeq;
    public String Description;
    public String messageTS;
    public Integer messageLevel;
    public Boolean isSystemMessage;
    // parsing fields
    private ResponseWrapper theResponse;
    private JSONParser parser;

    public void parse(String jsonString){
        parser = JSON.createParser(jsonString);

    private void parseJSON(){   
        while (parser.nextToken() != null) {
            if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
                theResponse = (ResponseWrapper)parser.readValueAs(ResponseWrapper.class);               

String responseString = '{"MessageSeq":13,"Description":"config file: config-qa1.txt\"","MessageTS":"2014-02-17T14:46:58","MessageLevel":4,"IsSystemMessage":false}';
system.debug('\n\nBEFORE\n' + responseString);
****Place code in here to test such as ReplaceALL***
ResponseWrapper parser = new ResponseWrapper();
share|improve this question
This is syntactically correct and validates at What exception are you getting? – David Feb 17 '14 at 21:21
Are you sure the exception is caused by the \" ? including the exception message in your question may help. – Samuel De Rycke Feb 17 '14 at 21:28
16:36:15.863 (863767000)|FATAL_ERROR|System.JSONException: Unexpected character ('"' (code 34)): was expecting comma to separate OBJECT entries at [line:1, column:3699] Pretty much the string looks like such to the system and it fails to parse at that point. "config file: config-qa1.txt"","MessageTS" – Double A Feb 17 '14 at 21:36
Exception included – Double A Feb 17 '14 at 21:46
If you are using Apex to parse, have a look at .. Your posted example parses fine, but removing the escape character duplicates your error. Maybe try double escaping ?? – David Feb 17 '14 at 22:08
up vote 7 down vote accepted

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);

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_______________________________________________^
share|improve this answer
exactly! added sample code which throws same error, I'm testing this on eclipse currently – Double A Feb 17 '14 at 22:30
@user320 This is a perfectly normal situation. Every known language that uses backslash to escape characters in a string, and does not have a "bare string" syntax suffers from this. For example, in C#, to reference a file on a windows server, you might write \\\\Server\\Path\\Filename.ext, which translates to \\Server\Path\Filename.ext. Notable exceptions include (I believe) scripting languages, like Perl, Ruby, and PHP. Some languages also offer a "anti-escape" operator, like C#'s @ symbol (assuming I'm not mixing up my syntaxes...) – sfdcfox Feb 17 '14 at 23:44
So what would be the recommendation you have for me to resolve this issue? I'm unsure how I'm going to get that slash in there as I don't generate the response when I make the callout. I provided an example from the callout which was causing an issue. – Double A Feb 18 '14 at 1:52
@DoubleA I think your code String responseString is not representative of the actual response. The callout response, as received, parses as expected. But when it is hand-imitated as a string without escaping the backslash \\ it becomes a different string. Pardon, I am struggling to explain :-) one backslash is already in the response, it should remain there, and you don't need to add anything. – bigassforce Feb 18 '14 at 2:35
@user320 yes, but if you run the sample code you will get the exception i'm currently getting. the section I provided as a string is a collection of that same object which gives backend information such as malformed parameters, exceptions, timeouts, errors, etc. I just pulled this section out of the response which is causing me this issue – Double A Feb 18 '14 at 2:40

If you want to continue using dphil's idea, then '\\\"' (3 slashes) might work in the replace statement. However, your original problem is still odd.

share|improve this answer
already attempted this too – Double A Feb 17 '14 at 22:15
added sample code which throws same error, I'm testing this on eclipse currently – Double A Feb 17 '14 at 22:29

It doesn't look like that belongs there.... Maybe you should just parse it out before using the JSONParser?

jsonString = jsonString.replace('\\"', '');
share|improve this answer
Does not work. This will only remove all double quotes. You can test this out via developer console. If you debug your line before and after you'll see the ending result – Double A Feb 17 '14 at 21:37
What I had was String response = ' "MessageSeq":13,"Description":"config file: config-qa1.txt\"","MessageTS":"2014-02-17T14:46:58","MessageLevel":4,"IsSystemMe‌​ssage":false'; system.debug('\n\nBEFORE\n' + response); system.debug('\n\nAFTER\n' + CommonSystemMethods.sanitizeJSONString(response)); public static String sanitizeJSONString(String jsonString){ jsonString = jsonString.replaceAll('\n', ''); jsonString = jsonString.replaceAll('\t', ''); return jsonString; } – Double A Feb 17 '14 at 21:41
added sample code which throws same error, I'm testing this on eclipse currently – Double A Feb 17 '14 at 22:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.