I'm trying to escape a string but I had no success so far.

This is the response body:

{"text":"this \"is something\" I wrote"}

Please note that there are 2 backslashes to escape the double quotes char. (This is a sample. Actually I have a big to escape with lots of "text" elements.) When I try to deserialize it I get the following error:

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

I've tried to escape by using:

String my = '{"text":"this \"is something\" I wrote"}';
System.debug('test 0: ' + my);
System.debug('test 1: ' + my.replace('\"', '-'));
System.debug('test 2: ' + my.replace('\\"', '-'));
System.debug('test 3: ' + my.replace('\\\"', '-'));
System.debug('test 4: ' + my.replace('\\\\"', '-'));

--- Results:

[22]|DEBUG|test 0: {"text":"this "is something" I wrote"}
[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}
[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}
[24]|DEBUG|test 2: {"text":"this "is something" I wrote"}
[25]|DEBUG|test 3: {"text":"this "is something" I wrote"}
[26]|DEBUG|test 4: {"text":"this "is something" I wrote"}

Note that in test 0 Salesforce automatically escaped my string. --- What I need as result:

{"text":"this -is something- I wrote"}

Please, does someone has any fix to share?

Thanks a lot.

  • What's the actual problem you're trying to solve? Is it the JSON parsing of your string, or the regex replace on your string? Your examples show nothing about JSON. – Nick Cook Feb 6 at 5:42
  • Have you tried the method I showed below? I have verified it works. – Caspar Harmer Feb 8 at 22:30

When in doubt, you can just iterate through the chars. It's not pretty, but I was able to get it to work like this:

Edit: OK, Dang, you are right. Looks like Apex can't even see that char.

Try this SUPER hack.

String str = '{"text":"this \"is something\" I wrote"}';
str = str.replace(' ', '^^^');
Boolean done = false;
Integer count = 0;
Map<String,Object> jsonMap;
while (!done){
    if (count > 10){
        done = true;
  try {
    jsonMap = (Map<String,Object>)JSON.deserializeUntyped(str); 
    done = true; 
    catch(Exception e){
    String msg = e.getMessage();
    String s2 = msg.substringBetween('[',']');
    Integer errorLocation = Integer.valueOf(s2.substring( ( s2.lastIndexOf(':')+1)));   
    String str1 = str.substring(0, errorLocation-3);
    String str2 = str.substring(errorLocation-2);
    str = str1 + '-' + str2;
str = str.replace('^^^', ' ');

Makes me feel ill reading that, but it works.

(I'm parsing the exception error to get the column that caused the issue, replacing that column and so on until done)

  • Hi Caspar! Thanks for replying. Your code generated this output: {"text":"this "is something" I wrote"} , but the one I'm looking for is this, without the double quotes besides the "is something" phrase: {"text":"this is something I wrote"} – Israel Agostinho Feb 9 at 23:13
  • Ok, try my next doozie. Horrible, horrible code. But it works. – Caspar Harmer Feb 10 at 1:54

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.