I'm new to apex rest services.Normally in Apex rest services the input json body automatically deserializes to apex class object. but my json body contains reserved key words.For Rest services automatically deserialization happen if we define method parameters so we don't have control to replace json string.

Can anyone please let me know how to overcome this problem.

I'll receive input in the following way











































please help me how to deserialize the above string in apex rest service


You can use JSON class.

If you have a model you can use JSON.deserialized Ex

String myJson = '{name="Test",phone="1234567890"}';
Account ac = (Account) JSON.deserialize(myJson, Account.class);

If you dont want use a model then you can use JSON.deserializeUntyped Ex:

String myJson = '{name="Test",phone="1234567890"}';
Map<String,Object> ac = JSON.deserializeUntyped(myJson);

Best Regards


The easiest way I've found it to alter the body of the response before deserializing. It's not ideal but I'm not sure there's a better way.

So, take 'date' for example. This is reserved & cannot be used as a class member name.

// We need to change the name of the 'date' member to 'date_x'
String responseBody = httpResponse.getBody().replaceAll('\"date\":', '\"date_x\":');

Then you can deserialize into your object as normal, but using the string responseBody instead.

MyResponseObject myResponse = (MyResponseObject) JSON.deserializeStrict(responseBody, MyResponseObject.class);

Obviously you will need to alter the member name in your response object to use the new date_x name:

public class MyResponseObject {

    public Date date_x;

Not the answer you're looking for? Browse other questions tagged or ask your own question.