According to this post, returning a blank list should handle nulls. But in my case it still complains that it's expecting something but found null. Here is my class:

public class PersonProfile {

        public class PhoneDetails {
            public String cust_phone_id;
            public String phone_type;
            public String phone_type_primary_ind;
            public String phone_number;
            public String solicit_phone;
            public String mobile_text_messages_allowed;

        public List<RECORD> RECORD;

        public class AddressDetails {
            public String cust_address_id;
            public String address_type;
            public String primary_ind_address;
            public String address_type_primary_ind;
            public String street_addr_1;
            public String street_addr_2;
            public String city;
            public String state;
            public String zip;
            public String country;
            public String company_address_ind;
            public String solicit_mail;

        public class RECORD {
            public String cust_name_id;
            public String email_id;
            public String email_type;
            public String email_addr;
            public String primary_ind_email;
            public String solicit_email;
            public String name_type;
            public String name_first;
            public String name_middle;
            public String name_last;
            public String full_name;
            public String name_suffix;
            public String name_title;
            public String company_name;
            public String gender;
            public String add_datetime;
            public String upd_datetime;
            public List<PhoneDetails> PhoneDetails { get {
                return PhoneDetails == null ? new List<PhoneDetails>() : PhoneDetails;
                } set;
            public List<AddressDetails> AddressDetails { get {
                return AddressDetails == null ? new List<AddressDetails>() : AddressDetails;
                } set;

Here is the JSON:

    "RECORD": [
            "cust_name_id": "3594720",
            "name_type": "I",
            "name_first": "Count",
            "name_middle": null,
            "name_last": "Ravioli",
            "full_name": "Count Ravioli",
            "name_suffix": null,
            "name_title": null,
            "company_name": null,
            "add_datetime": "2020-01-15T09:59:20.173-05:00",
            "upd_datetime": "2020-01-15T09:59:20.173-05:00",
            "PhoneDetails": [
            "AddressDetails": [
                    "cust_address_id": "4539116",
                    "address_type": "H",
                    "primary_ind_address": "Y",
                    "address_type_primary_ind": "Y",
                    "street_addr_1": null,
                    "street_addr_2": null,
                    "city": null,
                    "company_address_ind": "N",
                    "solicit_mail": "N"

And here is how i call it:

public static IntegrationWebService.Response createProfileRegistration(RestRequest req, RestResponse res) {

    String errorMessage;

    Exception exceptionResult;
    Boolean errorsFound = false;

    try {
        PersonProfile  profile = new PersonProfile ();
        profile = (PersonProfile ) System.JSON.deserialize(req.requestBody.toString(), PersonProfile.class);

        for (PersonProfile.RECORD p : profile.RECORD){      

        //Do a bunch of things...

    catch(Exception e){
        System.debug('ERROR: ' + e);
        String exType = e.getTypeName();
        String exMessage = e.getMessage();

        exceptionResult = e;
        errorsFound = true;        

this line throws the error, however, if i remove the null and pass an empty array it doesn't complain at all and processes the record. How can i properly handle nulls without having to pass empty arrays?

 profile = (PersonProfile ) System.JSON.deserialize(req.requestBody.toString(), PersonProfile.class);

The error is: "errorMessage": "Expected PersonProfile.PhoneDetails but found null at [line:16, column:5]"

  • an empty array would be the proper semantics or simply a value of null for property PhoneDetails. Do you have control over the input JSON?
    – cropredy
    Jan 22, 2020 at 21:48
  • Yes, i do have control of the input. So best practice would be to pass a blank array instead of null?
    – SavvyGoat
    Jan 22, 2020 at 21:53
  • yep --- either empty array or no value for the property at all (and your getter transforms to an empty array)
    – cropredy
    Jan 22, 2020 at 21:55
  • @cropredy You should have added an answer to that effect?
    – sfdcfox
    Jan 23, 2020 at 1:50

1 Answer 1


As you have control over the input JSON, there are three ways to express an empty list:

Option 1

        "PhoneDetails": [],

in which case the value of the property PhoneDetails will be an empty list of PhoneDetails objects (as an aside, this inner class should be called PhoneDetail as it is an object of a single Phone)

Option 2

        "PhoneDetails": null,

Option 3 - simply omit the PhoneDetails property altogether in the payload - it will deserialize as null into the PhoneDetails property within the parent RECORD object.

In Option 2 and 3, you need a getter on the PhoneDetails property as you have coded in the OP:

public List<PhoneDetails> PhoneDetails { 
   get {
        return PhoneDetails == null ? new List<PhoneDetails>() : PhoneDetails;
        } set;

As a matter of defensive programming, I would assume Option 3 in the Apex and that Options 1-2-3 are all acceptable and will deserialize correctly. Anything else is a violation of the JSON contract you have between the publisher and consumer of this payload.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .