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?

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

    This is syntactically correct and validates at http://jsonlint.com/ What exception are you getting?

    Are you sure the exception is caused by the \" ? including the exception message in your question may help.

    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"

    Exception included

    If you are using Apex to parse, have a look at http://json2apex.herokuapp.com/ .. Your posted example parses fine, but removing the escape character duplicates your error. Maybe try double escaping ??

    something like \\\"

    Already tried this too. Still have issues. Let me code up something simple for you all to test this on

    added sample code which throws same error, I'm testing this on eclipse currently

    added sample code which throws same error, I'm testing this on eclipse currently

  • 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_______________________________________________^

    exactly! added sample code which throws same error, I'm testing this on eclipse currently

    @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...)

    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.

    @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.

    @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

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used