I've set up an @RestResource class for receiving an external JSON webhook through an @httppost class, which is working great, but when retrieving the content of the request through RestContext.request.requestBody.toString(), escaped special characters within JSON strings, specifically \", appear to be unescaping to ", which then prevents JSON.deserialize() from correctly parsing the string.

Example code:

global class listener {

   public class parseJSON {
       public string field;

    global static void postmethod(){
        JSON.deserialize(RestContext.request.requestBody.toString(), parseJSON.class);

In this example, a request body of:{"field":"value"} will succeed.

However, a request body of {"field":"value \" "}, produces the following error:

FATAL_ERROR System.JSONException: Unexpected character ('"' (code 34)): was expecting comma to separate OBJECT entries at [line:1, column:20]`

Am I approaching/understanding this correctly?

  • How are you hitting this endpoint? Passing the second payload in through the Developer Workbench gives me no error.
    – Adrian Larson
    Oct 13 '17 at 3:37
  • Thank you! It was in fact just a silly issue with the encoding of the request, not sending properly escaped characters in the first place, I can't believe I didn't think to check that.
    – mat
    Oct 13 '17 at 3:55

Seems like an issue with how you send the payload. When I send either payload to an identical endpoint, I get a successful request.

  • Should I be marking this answer as correct or just deleting the question entirely, given that there does not appear to be any issue on the salesforce side?
    – mat
    Oct 13 '17 at 3:58
  • It's certainly your choice. I see no problem with leaving it, as it was well asked and included all relevant details. That way it reflects positively on your account. Having a deleted question does rather the opposite.
    – Adrian Larson
    Oct 13 '17 at 3:59
  • 1
    Well, in that case, I would much prefer to be reflected positively when possible. Thanks again!
    – mat
    Oct 13 '17 at 4:03
  • Welcome to SFSE! Cheers!
    – Adrian Larson
    Oct 13 '17 at 4:04

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.