Please hear me out before marking this as duplicate or irrelevant.

I have done one integration and I am getting data something like this.

  "page_number": 1,
  "page_size": 10,
  "total_elements": 141,
  "last": false,
  "result": [
      "triggered_at": 1563380205046,
      "user_id": "100-080-0982",
      "type": "freeform_content_push",
      "content": "{\"GCM\":\"{\\\"data\\\":{\\\"type\\\":\\\"content-push\\\",\\\"mode\\\":\\\"TAGGED\\\",\\\"content_type\\\":\\\"TEXT\\\",\\\"content\\\":\\\"the body\\\",\\\"title\\\":\\\"the title\\\",\\\"expiration\\\":null,\\\"deeplink\\\":null,\\\"template_id\\\":\\\"f8c29e89-253f-41df-8b7e-5d4a7ab8e539\\\"}}\",\"APNS\":\"{\\\"aps\\\":{\\\"content-available\\\":1},\\\"type\\\":\\\"content-push\\\",\\\"mode\\\":\\\"TAGGED\\\",\\\"content_type\\\":\\\"TEXT\\\",\\\"content\\\":\\\"the body\\\",\\\"title\\\":\\\"the title\\\",\\\"expiration\\\":null,\\\"deeplink\\\":null,\\\"template_id\\\":\\\"f8c29e89-253f-41df-8b7e-5d4a7ab8e539\\\"}\",\"APNS_SANDBOX\":\"{\\\"aps\\\":{\\\"content-available\\\":1},\\\"type\\\":\\\"content-push\\\",\\\"mode\\\":\\\"TAGGED\\\",\\\"content_type\\\":\\\"TEXT\\\",\\\"content\\\":\\\"the body\\\",\\\"title\\\":\\\"the title\\\",\\\"expiration\\\":null,\\\"deeplink\\\":null,\\\"template_id\\\":\\\"f8c29e89-253f-41df-8b7e-5d4a7ab8e539\\\"}\"}"

Now I want to parse the "content" value but as you see the content is coming as String. So first I have removed all the \ characters from the payload.

Now I have something like this

"{"GCM":"{"data":{"type":"content-push","mode":"TAGGED","content_type":"TEXT","content":"the body","title":"the title","expiration":null,"deeplink":null,"template_id":"f8c29e89-253f-41df-8b7e-5d4a7ab8e539"}}","APNS":"{"aps":{"content-available":1},"type":"content-push","mode":"TAGGED","content_type":"TEXT","content":"the body","title":"the title","expiration":null,"deeplink":null,"template_id":"f8c29e89-253f-41df-8b7e-5d4a7ab8e539"}","APNS_SANDBOX":"{"aps":{"content-available":1},"type":"content-push","mode":"TAGGED","content_type":"TEXT","content":"the body","title":"the title","expiration":null,"deeplink":null,"template_id":"f8c29e89-253f-41df-8b7e-5d4a7ab8e539"}"}"}

There are few extra double qoutes in the given json and it will work if I will replace "{" => {" and }" => }.

But whenever I am trying to do jsonResponse = jsonResponse.replaceAll('"{"', '{"'); this line it gives me an error

FATAL_ERROR System.StringException: Invalid regex: Illegal repetition near index 0 "{"

Please help me here. I have tried several ways to do this but still error only.

I you have some other and better way to parse this please let me know.

Thank you so much for your time and efforts.


The system that is sending you this payload is broken. The best way to address this problem is to fix the remote system so that it sends proper JSON.

To address your question as asked, though: you are receiving this error because the first parameter of replaceAll() is a Java regular expression, not a string literal. The { character is being interpreted here as a quantifier, the start of an expression like "{3} which would match a character 3 times. You need to escape it with a backslash, which then gets an extra backslash when present in an Apex string literal since it too is a special character.

jsonResponse = jsonResponse.replaceAll('"\\{"', '{"');
| improve this answer | |
  • Yes just now I also figured it out. Btw thanks for the help. Yes I am aware of broken payload but its coming from third party and not in my hands so no other option than doing some workaround. – Nupur Bhagat Aug 2 at 22:22

You just need to deserialize the content again:

Map<String, Object> payload = (Map<String, Object>)JSON.deserializeUntyped(responseJson);
List<Object> results = (List<Object>)payload.get('result');
for(Object result: results) {
    Map<String, Object> resultContent = (Map<String, Object>)result;
    Map<String, Object> content = JSON.deserializeUntyped((String)resultContent.get('content'));

You may be tempted to just use replaceAll and be done with it, but you may break unexpectedly. Anything other than using a JSON parser is asking for trouble. You have been warned.

| improve this answer | |
  • I tried that way as well. but the payload is incorrect so I am getting error while deserializing it into Map. – Nupur Bhagat Aug 3 at 7:41

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.