Iterating through JSON response from HubSpot deals endpoint

I’m working with the HubSpot API to iterate through all deals and extract specific information from each one. I want to filter deals based on their current status and then process them accordingly.

The main issue I’m facing is properly accessing the nested data structure. The API returns data where each property like deal status, value, and other fields are stored in arrays with a ‘value’ key.

My goal is to loop through all deals and extract multiple properties for each deal including status, value, last modified date, owner information, and more. Each property follows the same nested structure.

Currently I can extract the deal status using this approach:

foreach ($response['deals'] as $deal) {
    foreach ($deal['properties'] as $property => $data) {
        if ($property == "deal_status") {
            $status = $data['value'];
            echo $status . "<br>";
        }
    }
}

Here’s a sample of the data structure I’m working with:

Array
(
    [deals] => Array
        (
            [0] => Array
                (
                    [portalId] => 98765
                    [dealId] => 12345
                    [isDeleted] => 
                    [associations] => Array
                        (
                            [associatedVids] => Array
                                (
                                    [0] => 5678
                                )
                            [associatedCompanyIds] => Array
                                (
                                    [0] => 34567
                                )
                        )
                    [properties] => Array
                        (
                            [deal_status] => Array
                                (
                                    [value] => won
                                )
                            [deal_value] => Array
                                (
                                    [value] => 2500
                                )
                            [created_at] => Array
                                (
                                    [value] => 1471334633784
                                )

Is there a more efficient way to extract multiple properties from each deal without having to check each property name individually?

array_map is the way to go! Just set up a quick helper function to get the values:

function extractDealData($deal) {
    $props = $deal['properties'];
    return [
        'status' => $props['deal_status']['value'] ?? null,
        'value' => $props['deal_value']['value'] ?? null,
        'created' => $props['created_at']['value'] ?? null
    ];
}

$cleanDeals = array_map('extractDealData', $response['deals']);

So much easier than looping!

I hit the same nested structure nightmare with HubSpot’s API. Here’s what saved me - a simple property extraction function that cuts out all the repetitive work:

function getProperty($deal, $propertyName) {
    return $deal['properties'][$propertyName]['value'] ?? null;
}

foreach ($response['deals'] as $deal) {
    $status = getProperty($deal, 'deal_status');
    $value = getProperty($deal, 'deal_value');
    $created = getProperty($deal, 'created_at');
    $owner = getProperty($deal, 'hubspot_owner_id');
    
    // Your processing logic here
}

This kills the need to repeat the same array access pattern everywhere. The null coalescing operator handles missing properties without throwing errors. I’ve processed thousands of deals this way - it’s rock solid and way cleaner than nested loops or hardcoded isset checks for every property.

Skip the individual property iteration - just grab what you need directly. Since you know the structure, pull multiple values in one go:

foreach ($response['deals'] as $deal) {
    $properties = $deal['properties'];
    
    $status = isset($properties['deal_status']) ? $properties['deal_status']['value'] : null;
    $value = isset($properties['deal_value']) ? $properties['deal_value']['value'] : null;
    $created = isset($properties['created_at']) ? $properties['created_at']['value'] : null;
    
    // Process your extracted data here
    echo "Status: $status, Value: $value, Created: $created<br>";
}

This cuts out the nested foreach and goes straight to the properties you want. The isset() checks stop errors when a property’s missing. I’ve used this pattern tons with HubSpot’s API - way cleaner and faster than checking each property name separately.