Exclude Null Data in REST API Response to Improve Readability

As an integration user, when you retrieve the details of a record there could be data that you don't need, such as attributes with null data or attributes having a 'false' value. For example, suppose you are retrieving the details of an account, you may come across attributes of this record with null data such as 'emailNotification' or attributes having a 'false' value such as 'accountLocked' that you may want to exclude.

To exclude these attributes, you can use the 'prefer' header with the value 'exclude-null-properties' in your REST API call. However, before you decide to exclude the null data, you may want to verify the data that will get excluded. You can use the following steps to go about this task:

  1. Retrieve All Data
  2. Retrieve Reduced Data Without the Null Attributes
    • More compact response
    • Improved data readability
  3. Compare both the responses to identify the data that will get excluded.
  4. Decide whether you want to exclude null data.

Retrieve All Data

Suppose you want to retrieve all the details for an account having ID as 1. You can retrieve the record details as follows:

  1. Construct the request URL for the Get an account operation
  2. Run the request using the GET HTTP method to retrieve the account details

Example URL

Here's the complete URL with the GET HTTP method.

GET
https://mysite.example.com/services/rest/connect/v1.4/accounts/1

cURL Command

curl -X GET http://mysite.example.com/services/rest/connect/v1.4/accounts/1 -H 'Authorization: Basic YWRtaW46' -H 'OSvC-CREST-Application-Context: Retrieve account'

Example Response

Here's an example of the response body in JSON format.

{
    "id": 1,
    "lookupName": "Administrator -",
    "accountHierarchy": {
        "links": [
            {
            "rel": "self",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1/accountHierarchy"
            }
        ]
    },
    "attributes": {
        "accountLocked": false,
        "canModifyEmailSignature": false,
        "forcePasswordChange": false,
        "infrequentUser": false,
        "passwordNeverExpires": false,
        "permanentlyDisabled": false,
        "staffAssignmentDisabled": true,
        "viewsReportsDisabled": true,
        "virtualAccount": false
    },
    "country": null,
    "customFields": {
        "CO": {
            "ca_ac_date": null,
            "ca_ac_integer": null,
            "ca_ac_menu": null,
            "ca_ac_radio": null,
            "ca_ac_text": null,
            "ca_ac_text_email": null,
            "ca_ac_text_long": null,
            "ca_ac_text_url": null,
            "ca_ac_time": null
        },
        "CustParent": {
            "CPObject3ID": null
        },
        "SelfPackage": {
            "AccountID": null
        },
        "c": {
            "ac_text": null,
            "ac_text_url": null,
            "ac_text_email": null,
            "ac_text_phone": null,
            "ac_date": null,
            "ac_time": null,
            "ac_integer": null,
            "ac_menu": null,
            "ac_radio": null,
            "ac_area": null,
            "ac_display": null,
            "ac_novis": null,
            "ac_special_menu": null,
            "ac_max_menu": null,
            "ac_folder_text": "4061234567",
            "ac_folder_text_url": "http://example.com",
            "ac_folder_text_email": "nobody@example.com",
            "ac_folder_text_phone": "4065224200",
            "ac_folder_date": null,
            "ac_folder_time": null,
            "ac_folder_integer": 26,
            "ac_folder_menu": {
                "id": 47,
                "lookupName": "Dolphin"
            },
            "ac_folder_yes_no": null
        }
    },
    "displayName": "Administrator",
    "emailNotification": null,
    "emails": {
        "links": [
            {
            "rel": "self",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1/emails"
            }
        ]
    },
    "login": "administrator",
    "manager": null,
    "name": {
        "first": "Administrator",
        "last": "-"
    },
    "nameFurigana": {
        "first": null,
        "last": null
    },
    "notificationPending": false,
    "passwordExpirationTime": "2020-12-31T09:00:00.000Z",
    "phones": {
        "links": [
            {
            "rel": "self",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1/phones"
            }
        ]
    },
    "profile": {
        "id": 1,
        "lookupName": "Full Access"
    },
    "salesSettings": {
        "defaultCurrency": {
            "id": 1,
            "lookupName": "USD"
        },
        "territory": null
    },
    "serviceSettings": {},
    "signature": null,
    "staffGroup": null,
    "links": [
        {
        "rel": "self",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1"
        },
        {
        "rel": "canonical",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1"
        },
        {
        "rel": "describedby",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/metadata-catalog/accounts"
        },
        {
        "rel": "alternate",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1",
        "mediaType": "application/schema+json"
        }
    ]
}

Retrieve Reduced Data Without the Null Attributes

You can retrieve the account record having ID as 1 excluding attributes with null data as well as attributes having a value of 'false' as follows:

  1. Construct the request URL for the Get an account operation
  2. Specify the value of the 'prefer' header as 'exclude-null-properties'
  3. Run the request using the GET HTTP method to retrieve the account details

Example URL

Here's the complete URL with the GET HTTP method. And the value of the 'prefer' header is set to 'exclude-null-properties'.

GET
https://mysite.example.com/services/rest/connect/v1.4/accounts/1

cURL Command

curl -X GET http://mysite.example.com/services/rest/connect/v1.4/accounts/1 -H 'Authorization: Basic YWRtaW46' -H 'OSvC-CREST-Application-Context: Retrieve account' -H 'prefer: exclude-null-properties'

Example Response

Here's an example of the response body in JSON format.

{
    "id": 1,
    "lookupName": "Administrator -",
    "accountHierarchy": {
        "links": [
        {
            "rel": "self",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1/accountHierarchy"
            }
        ]
    },
    "attributes": {
        "staffAssignmentDisabled": true,
        viewsReportsDisabled": true
        },
    "customFields": {
        "CO": {},
        "CustParent": {},
        "SelfPackage": {},
        "c": {
            "ac_folder_text": "4061234567",
            "ac_folder_text_url": "http://example.com",
            "ac_folder_text_email": "nobody@example.com",
            "ac_folder_text_phone": "4065224200",
            "ac_folder_integer": 26,
            "ac_folder_menu": {
                "id": 47,
                "lookupName": "Dolphin"
                }
            }
    },
    "displayName": "Administrator",
    "emails": {
        "links": [
            {
            "rel": "self",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1/emails"
            }
        ]
    },
    "login": "administrator",
    "name": {
        "first": "Administrator",
        "last": "-"
    },
    "nameFurigana": {},
    "passwordExpirationTime": "2020-12-31T09:00:00.000Z",
    "phones": {
        "links": [
            {
            "rel": "self",
            "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1/phones"
            }
        ]
    },
    "profile": {
        "id": 1,
        "lookupName": "Full Access"
    },
    "salesSettings": {
        defaultCurrency": {
            "id": 1,
            "lookupName": "USD"
        }
    },
    "serviceSettings": {},
    "links": [
        {
        "rel": "self",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1"
        },
        {
        "rel": "canonical",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1"
        },
        {
        "rel": "describedby",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/metadata-catalog/accounts"
        },
        {
        "rel": "alternate",
        "href": "https://mysite.example.com/services/rest/connect/v1.4/accounts/1",
        "mediaType": "application/schema+json"
        }
    ]
}

Note:

Compare both the responses and decide whether you want to exclude the null attributes. Notice how the response for the request with the 'prefer' header excludes data for as many as 44 attributes!