function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RanlouRanlou 

Getting a JSON string to parse with Apex

I am trying to receive a JSON string in salesforce by converting a blob in the body of an Http request. However, when I convert the blob to a string, \ characters get inserted before the quotation marks, which prevents me from parsing.

I tried to take the string and remove all \ characters by using the string.removeAll() method... but that didn't work either.

 

    RestRequest req =RestContext.request;
   
Blob jsonBlob = req.requestBody;
   
String jsonString = jsonBlob.toString();
   
return jsonString;


The original string (the one that is received as a blob) looks like this:

 

    {"putTimeCard":{"timecard":{"timeCardID":"","employeeID":""}}


And after converting to a salesforce string and assigned to the jsonString is altered to:

 

    {\"putTimeCard\":{\"timecard\":{\"timeCardID\": \"\",\"employeeID\": \"\"}}

 

Has anyone found a solution for this? Thanks

Starz26Starz26

Just use the resp.getBody(). no need to convert it to a blob..

 

Or String s = resp.getBody();

 

(YOUROBJECTCLASS)JSON.deserialize(resp.getBody(),YOUROBJECTCLASS)
Starz26Starz26

As far as your replacing how are you doing it?

 

it should be:

 

jsonString = jsonString.replace('\\');