This string is part of a JSON that I have. I am trying to store the JSON string that was generated using JSON.serialize method. Part of the JSONString has "default_value": "\" \"" as one of the JSON key value pair.

String bodyStr = '"default_value": "\"   \""';

Outputs - "default_value": "" ""

How can I preserve the string as it is - "default_value": "\" \"" . I tried looking into String Apex class but could not find a method that supports this outcome.


Double quotes are not special characters in Apex and don't need to be escaped. Backslashes, however, are, and do need to be escaped. If you do

String bodyStr = '"default_value": "\\"   \\""';

You will in fact get back

11:09:50:003 USER_DEBUG [2]|DEBUG|"default_value": "\" \""

  • I do not have control over the String. Actually that string I get using JSON.serialize method and I want to preserve it as it is. – SfdcBat Feb 14 at 16:11
  • What do you mean by preserve it? Just in a logging context or otherwise? – David Reed Feb 14 at 16:13
  • Meaning I would get String bodyStr = JSON.serialize(apexObj); . Then I wish to save this as a blob. I was wondering if the output of JSON.serialize(apexObj) has "default_value": "\\" \\"" then would it preserve it in the blob that I create? – SfdcBat Feb 14 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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