Extracting values from HubSpot API response in PHP

I’m dealing with data retrieved from HubSpot’s API and I need assistance in accessing particular values from the JSON response. After decoding, I’m left with a complicated nested structure like this:

stdClass Object(
[contactId] => 45
[createdAt] => 1512345678901
[portalId] => YYYYY
[contactData] => stdClass Object
    (
        [email] => stdClass Object
            (
                [content] => [email protected]
            )
        [company] => stdClass Object
            (
                [content] => Sample Corp
            )
    )
[userProfiles] => Array
    (
        [0] => stdClass Object
            (
                [profileId] => 45
                [contacts] => Array
                    (
                        [0] => stdClass Object
                            (
                                [category] => EMAIL
                                [content] => [email protected]
                                [created] => 1512345678901
                            )
                        [1] => stdClass Object
                            (
                                [category] => VISITOR_ID
                                [content] => abc123-def456-ghi789
                                [created] => 1512345678902
                            )
                    )
            )
    )
)

I aim to retrieve the email address located in the userProfiles section. When I try this code:

echo $response->contacts[0]->user-profiles;

It simply returns 0. Then, I attempt:

echo $response->contacts[0]->user-profiles[0];

But I encounter a syntax error. What is the correct way to access nested properties that have hyphens in PHP? I need to navigate to userProfiles[0]->contacts[0]->content to extract the email value.

You’ve got a syntax issue with the property names. When PHP object properties have hyphens, you need curly braces to access them. But looking at your data structure, the property is actually userProfiles (not user-profiles) and you’re trying to access it on the wrong object.

Here’s the correct way to get the email from userProfiles:

echo $response->userProfiles[0]->contacts[0]->content;

You were trying $response->contacts[0] but there’s no contacts property at the root level. The userProfiles array sits directly under the main response object. And if you ever need to access properties with hyphens, use $object->{'property-name'} instead of dot notation.

heads up - your code has $response->contacts[0]->user-profiles but there’s no contacts array at that level. try $response->userProfiles[0]->contacts[0]->content instead. also make sure you’re using the right variable name from your json decode - people sometimes forget the $data = json_decode($json) part

You’re pointing to the wrong path in your object structure. Your data shows userProfiles sits right at the root level, not inside a contacts array. I’ve dealt with HubSpot’s API before - this path mix-up happens all the time with their nested data. Try $response->userProfiles[0]->contacts[0]->content to grab that email. And if you need to filter by category, loop through the contacts array and check $contact->category === 'EMAIL' instead of assuming index 0 is always the email.