I have a JSON String as below.

String Input = {"body":"<style>p{margin-top:0px; margin-bottom:0px;}</style><body class=\"setupTab\"  style=\" background-color:#CCCCCC; bEditID:b1st1; bLabel:body;\">"}

When I try Map<String, Object> parsedJSONMap = (Map<String, Object>) JSON.deserializeUntyped(Input);

It throws error,

System.JSONException: Unexpected character ('s' (code 115)): was expecting comma to separate OBJECT entries at [line:1, column:76]

But if I change all \ to \\ in the String it works perfectly.

How can I replace all \ with \\ or is there any other way to achieve this?

String is generated locally not from any API.

1 Answer 1


This relates to the syntax of Apex strings where \ is an escape character that turns off any special meaning of the following character.

So to represent a literal \ you need to escape the special meaning of the second \ with an added first \, hence \\.

I suggest modifying your local code to not put in the \ in the first place as Apex strings use ' as the string delimiter not " so no need to escape ".

  • I'm fetching the String from Email Template HTML Body. So it includes ". Any other way to replace \ with \\?
    – Sunil C N
    Feb 11, 2021 at 10:44
  • Including " is OK. Does it also include the backslashes?
    – Keith C
    Feb 11, 2021 at 12:33
  • Yes. I see one backslash before each ". Maybe it's automatically added to escape quotes?
    – Sunil C N
    Feb 12, 2021 at 11:08
  • Maybe. I suggest .replace('\\', '') to get rid of them if you are sure the legitimate content will never have any backslashes.
    – Keith C
    Feb 12, 2021 at 11:13

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.