1

While making an API Call, my json String is containing \, which I want to remove. But I am not able to do so, by using the replaceAll method of String class. Can anyone please help?

The json request is malformed due to \ in between.

 DEBUG|Debug Log for Serialized JSON"{     \"Party\":{        \"PartyContacts\":[           {              \"State\":{                 \"ShortName\":\"GA\"            },            \"MailingAddress\":{                 \"AddressLine1\":\"605 Choctaw Circle\"            }         }      ],      \"PartyAddresses\":[           {              \"State\":{                 \"ShortName\":\"GA\"            },            \"PostalCode\":\"38751\",            \"IsMain\":\"True\",            \"IsForDocumentation\":\"True\",            \"HomeState\":{                 \"ShortName\":\"GA\"            },            \"HomePostalCode\":\"38751\",            \"HomeCity\":\"Indianola\",            \"HomeAddressLine1\":\"605 Choctaw Circle\",            \"City\":\"Indianola\",            \"AddressLine1\":\"605 Choctaw Circle\"         }      ]   }}"
2
  • 1
    Could you edit your question to show what you tried with String.replaceAll()? Where is the JSON coming from?
    – David Reed
    Commented Jun 29, 2019 at 11:33
  • I have this json stored in a text area field of a custom metadata Commented Jun 29, 2019 at 14:01

1 Answer 1

4

These backslashes are usually a sign of unintended double application of JSON serialization:

@IsTest
private class JsonTest {
    @IsTest
    static void test() {

        Map<String, Object> m = new Map<String, Object>{
            'a' => 'xxx',
            'b' => 'yyy'
        };

        // Normal JSON object with two properties.
        String jsonOnce = JSON.serialize(m);
        System.assertEquals('{"b":"yyy","a":"xxx"}', jsonOnce);

        // A single JSON string with the JSON string delimiter escaped inside.
        // A double backslash is needed in an Apex string to represent a backslash.
        String jsonTwice = JSON.serialize(jsonOnce);
        System.assertEquals('"{\\"b\\":\\"yyy\\",\\"a\\":\\"xxx\\"}"', jsonTwice);
    }
}

So best to not do that in the first place rather than try to remove the backslashes.

5
  • Hello Keith, but how could I proceed then? The callout is giving me an error of the improperly formatted json. Commented Jun 29, 2019 at 14:03
  • 2
    @ShobhitSaxena Look for the code that generates the JSON string you send out and change it so that there is no double escaping. You say it is stored: make sure the double escaping isn't there in the text you have stored (and if it is remove it) and also that you are not calling JSON.serialize on that string.
    – Keith C
    Commented Jun 29, 2019 at 14:24
  • Hello Keith, Ok I see. So, I think it should work if I trim out the white spaces from the reference string from the metadata where the request is stored Commented Jun 29, 2019 at 14:33
  • 1
    Thanks, Keith, for your valuable inputs. I got it working. I missed an observation that Json.serialize was not required when the Json is already stored. Commented Jun 29, 2019 at 14:39
  • 1
    @ShobhitSaxena Glad this helped.
    – Keith C
    Commented Jun 29, 2019 at 14:41

You must log in to answer this question.

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