"sourceMessage": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><error code=\"VALIDATION_FAILED\">    <failure cause=\"INVALID_VALUE\" field=\"Status\" value=\"NotDefined\"/></error>"

Encountered the error mentioned in the Title when JSON.deserialize(jsonReponse, ApexClasss.class)

Even though the double quote is escaped in the value in front of version. Can someone help?

1 Answer 1


If the JSON text is in Apex code like this you will need two backslashes:

private class JsonTest {

    private static String s = '"sourceMessage": "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><error code=\\"VALIDATION_FAILED\\">    <failure cause=\\"INVALID_VALUE\\" field=\\"Status\\" value=\\"NotDefined\\"/></error>"';

    static void test1() {

    static void test2() {
        JSON.deserializeUntyped('{' + s + '}');

as when the Apex is parsed \" maps to " (as the \ means ignore any special meaning of the following character) but you need to instead use \\" to map to \" on Apex parsing. Then the JSON parser at runtime sees an escaping backslash.

(I'm surprised test1 passes; sites like https://jsonformatter.curiousconcept.com don't consider that text to be valid JSON.)

  • Keith it solved my problem. Sep 20, 2018 at 13:51
  • @RakeshKumar That's good.
    – Keith C
    Sep 20, 2018 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .