HttpPost unable to parse LIST of JSON data

apexhttppostjsonrest

I'm testing this with the Salesforce workbench and I'm using

/services/apexrest/myRestRequest/doPost

with JSON data

{
    "contacts": [
        {
            "personEmail": "[email protected]",
            "firstName": "test0",
            "lastName": "test0",
            "recordTypeName": "PersonAccount",
            "phone": "0400000000"
        },
        {
            "personEmail": "[email protected]",
            "firstName": "test1",
            "lastName": "test1",
            "recordTypeName": "PersonAccount",
            "phone": "0411111111"
        }
    ]
}

and my APEX code is

@RestResource(urlMapping='/myRestRequest/*')
global with sharing class MyOwnTypeRestResource {
    
    @HttpPost
    global static void doPost(){
        RestRequest req = RestContext.request;
        
        List<contacts> contactsList = (List<contacts>)JSON.deserializeStrict(req.toString(), contacts.class);
        /*
         * Below line gives error of
         * Method does not exist or incorrect signature: void deserializeStrict(System.RestRequest, System.Type) from the type System.JSON
         * List<contacts> contactsList = (List<contacts>)JSON.deserializeStrict(req, contacts.class);
         */
  
        for (contacts c : contactsList){
            system.debug(c.firstName);
            system.debug(c.lastName);
            system.debug(c.personEmail);
            system.debug(c.phone);
            system.debug(c.recordTypeName);
        }
        
    }
    
    //Define Model
    global class contacts{
        public String firstName{get; set;}
        public String lastName{get; set;}
        public String personEmail{get;set;}
        public String phone{get;set;}
        public String recordTypeName{get;set;}        
    }
    
}

but when I execute it in the Workbench I get this error message

errorCode: APEX_ERROR
message: System.JSONException: Unexpected character ('R' (code 82)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2] (System Code) Class.MyOwnTypeRestResource.doPost: line 9, column 1

I've found multiple examples of how to cast a JSON List but I can't get it to work for myself. I think the issue is that I'm having to req.toString() to get JSON.deserialize to allow me to save the file, I'm using the developer console.

The end result I'm seeking is to be able to handle the JSON that is POSTed to a HttpPost by looping through it.

Thank you in advance

Matthew Harris

Best Answer

List<contacts> contactsList = (List<contacts>)JSON.deserializeStrict(req.toString(), contacts.class);

Should be:

request contactsList = (Request)JSON.deserializeStrict(req.requestBody.toString(), Request.class);

Calling toString() on the RestRequest itself will give you a completely different object that contains the request body, headers, etc, in a decidedly non-JSON style string.

In addition, the Request isn't an Array of Contacts wrappers, it's an object, so you need a wrapper for that, too:

public class Request {
  List<contacts> contacts;
}
Related Topic