Extracting email from HubSpot API's JSON response

I’m working on parsing data from the HubSpot API’s JSON response, specifically trying to retrieve an email from the identity profiles. The returned response is structured in a nested format that includes various details about the contacts.

stdClass Object(
[addedAt] => 1411052909604
[vid] => 24
[canonical-vid] => 24
[merged-vids] => Array
    (
    )
[portal-id] => XXXXX
[is-contact] => 1
[profile-token] => AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[profile-url] => https://app.hubspot.com/contacts/XXXXX/lists/public/contact/_AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[properties] => stdClass Object
    (
        [lastname] => stdClass Object
            (
                [value] => testtt
            )
        [firstname] => stdClass Object
            (
                [value] => test
            )
        [lastmodifieddate] => stdClass Object
            (
                [value] => 1411052906670
            )
    )
[form-submissions] => Array
    (
        [0] => stdClass Object
            (
                [conversion-id] => 85d24dd2-9ee9-4d47-b8f3-3035acbd8f3b
                [timestamp] => 1411052834097
                [form-id] => fb16efd9-23cc-4511-889c-204fc8b41dba
                [portal-id] => 401824
                [page-url] => http://wbl-1.hs-sites.com/test
                [canonical-url] => http://wbl-1.hs-sites.com/test
                [content-type] => landing-page
                [page-title] => test
                [page-id] => 1570433242
                [title] => Default Form (Sample)
                [first-visit-url] => http://wbl-1.hs-sites.com/test
                [first-visit-timestamp] => 1411052722970
                [meta-data] => Array
                    (
                    )
            )
    )
[list-memberships] => Array
    (
    )
[identity-profiles] => Array
    (
        [0] => stdClass Object
            (
                [vid] => 24
                [identities] => Array
                    (
                        [0] => stdClass Object
                            (
                                [type] => EMAIL
                                [value] => [email protected]
                                [timestamp] => 1411052834097
                            )
                        [1] => stdClass Object
                            (
                                [type] => LEAD_GUID
                                [value] => 0b6acf21-6cee-4c7b-b664-e65c11ee2d8e
                                [timestamp] => 1411052834201
                            )
                    )
            )
    )
[merge-audits] => Array
    (
    )
)

I’ve attempted to access the email using:

echo $result->contacts[0]->identity-profiles;

However, it yields a result of 0. When I delve deeper into the array:

echo $result->contacts[0]->identity-profiles[0];

I encounter a parse error. I’m unclear on the correct method to extract the value from identity-profiles[0]->identities[0]->value, which is expected to be [email protected]. What step am I missing?

had this exact issue yesterday lol. yeah, the hyphen’s def causing problems, but here’s what i do - check if the property exists before trying to access it:

if (property_exists($result->contacts[0], 'identity-profiles')) {
    $email = $result->contacts[0]->{'identity-profiles'}[0]->identities[0]->value;
}

HubSpot loves returning empty arrays or just missing properties entirely, so this saves you from fatal errors when the data’s not there.

Both solutions work, but you’ll keep hitting this parsing mess every time you pull from HubSpot or similar APIs.

I just automate the whole thing instead of fighting nested JSON constantly. Set up automation that hits the HubSpot API, grabs the email with proper syntax, and dumps it wherever you need - database, spreadsheet, whatever.

The automation handles all the property name headaches and runs automatically. No more manual parsing or syntax errors when they tweak the API structure.

For a quick fix, use the curly brace method:

$email = $result->contacts[0]->{'identity-profiles'}[0]->identities[0]->value;

But automating data extraction saves massive time long-term. I do this for all our CRM integrations.

You can also use property_exists() with isset() to safely navigate the nested structure. The hyphen isn’t the only problem - you need to make sure the whole chain exists first.

if (isset($result->contacts[0]->{'identity-profiles'}[0]->identities)) {
    foreach ($result->contacts[0]->{'identity-profiles'}[0]->identities as $identity) {
        if ($identity->type === 'EMAIL') {
            $email = $identity->value;
            break;
        }
    }
}

This works better because contacts have multiple identity types (EMAIL, LEAD_GUID, etc.) and EMAIL isn’t always at index 0. HubSpot returns them in different orders depending on how the contact was created. The loop finds the actual email identity instead of guessing its position.

You can also use PHP’s json_decode() with the associative array parameter set to true. This converts everything to arrays instead of objects and completely avoids the hyphen problem:

$data = json_decode($jsonResponse, true);
$email = $data['contacts'][0]['identity-profiles'][0]['identities'][0]['value'];
echo $email;

I prefer this method when APIs have messy naming conventions. The data structure becomes more predictable and you don’t have to deal with special characters in property names. Plus it’s way easier to debug with standard array syntax everywhere.

The problem is PHP treats identity-profiles as subtraction instead of a property name because of the hyphen. You need curly braces or array notation to access hyphenated keys.

Try this:

$email = $result->contacts[0]->{'identity-profiles'}[0]->identities[0]->value;
echo $email;

Or convert to an array first:

$contactArray = (array) $result->contacts[0];
$email = $contactArray['identity-profiles'][0]->identities[0]->value;
echo $email;

I’ve hit this same issue with APIs that use kebab-case. The curly brace method is usually cleanest for these tricky property names.