Skip to main content

Below is the response from an external system. In Salesforce, I need to save the value of test1. Now JSON Parser it trying to destruct the test1 value also because it has escape character before the double quotes. When I try to replace the \" with \\", it is replacing all the quotes with \\" instead of replacing the test1 value. Is there any other alternative way to parse this JSON Response?

 

#Apex  #Json  #Integration #Salesforce Developer

 

String s = '{ "test1": " {\"test1\":[{\"firstId\":\"Id1\"}]}", "test2" : "test2" }';

//This line throws an error since parser is trying to destruct the test1 value also.

//Map<String, Object> deserializedString = (Map<String, Object>) JSON.deserializeUnTyped(s);

//There won't be any slash in the output.

System.debug('s value ++ '+ s);

//s value ++ { "test1": " {"test1":[{"firstId":"Id1"}]}", "test2" : "test2" }

//It will replace all the double quotes with slash and double quotes. I just need to add an extra slash to the test1 value.

System.debug('After Replacing++ '+ s.replace('\"','\\"'));

//After Replacing++ { \"test1\": \" {\"test1\":[{\"firstId\":\"Id1\"}]}\", \"test2\" : \"test2\" }

1 件の回答
0/9000