thebiglebowski11
thebiglebowski11

Reputation: 1461

Receiving JSON in salesforce

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 there are \ characters that get inserted into the request which prevents me from parsing.

I then tried to take the string and remove all \ characters... 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

Upvotes: 2

Views: 6512

Answers (1)

Greg Grinberg
Greg Grinberg

Reputation: 665

The JSON Deserializer can parse the string with the escape characters. You can either deserialize into an object like so:

String jsonString = '{\"putTimeCard\":{\"timecard\":{\"timeCardID\": \"\",\"employeeID\": \"\"}}}'
Timecard t = (Timecard) JSON.deserialize(jsonString, Type.forName('Timecard'));

or if you just want a map of objects you can do the following:

String jsonString = '{\"putTimeCard\":{\"timecard\":{\"timeCardID\": \"\",\"employeeID\": \"\"}}}'
Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(jsonString);

Upvotes: 2

Related Questions