Iterating through HubSpot deals API JSON response data

I’m working with the HubSpot deals API and need to extract specific information from each deal record. The API returns a complex nested array structure where deal properties like status, value, and owner details are stored.

Currently, I can access one property at a time, but I want to efficiently extract multiple fields from each deal in a single loop. Here’s what I’m trying now:

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

The API response structure looks like this:

Array
(
    [deals] => Array
        (
            [0] => Array
                (
                    [portalId] => 54321
                    [dealId] => 98765
                    [properties] => Array
                        (
                            [deal_status] => Array
                                (
                                    [value] => active
                                )
                            [deal_value] => Array
                                (
                                    [value] => 2500
                                )
                            [last_modified] => Array
                                (
                                    [value] => 1471334633784
                                )

How can I better structure this to get multiple properties like status, value, modification date, and owner from each deal without writing separate if statements for each field?

API transformations for this stuff get old quick. HubSpot changes something or you need different fields? Back to updating loops again.

I quit writing custom PHP for this after getting burned by rate limits, pagination, and field mapping on several projects. Now I use automated workflows that handle the whole HubSpot data pipeline.

Pulls all deals, grabs whatever fields you want, transforms the format, sends it wherever. No more debugging nested arrays or broken code when properties go missing.

Workflow runs on schedule and handles API complexity automatically. Beats maintaining custom code that breaks every few months.

Latenode makes HubSpot automation pretty straightforward: https://latenode.com

array_map is perfect here - transforms the entire deals array without loops. Try array_map(function($deal) { return ['status' => $deal['properties']['deal_status']['value'], 'value' => $deal['properties']['deal_value']['value']]; }, $response['deals']) to grab everything at once. Much faster than processing each item separately.

Been there with HubSpot’s messy API responses. Skip the nested loop nightmare and automate it.

Make an array of your fields, then loop through:

$fields = ['deal_status', 'deal_value', 'last_modified', 'dealname'];
$extracted_data = [];

foreach ($response['deals'] as $deal) {
    $deal_info = [];
    foreach ($fields as $field) {
        $deal_info[$field] = $deal['properties'][$field]['value'] ?? null;
    }
    $extracted_data[] = $deal_info;
}

Manual API handling gets tedious though. I switched to automated workflows that pull, transform, and route HubSpot data without code.

Runs on schedule, grabs exactly what I need, sends it where it goes. No more array headaches or breaking when HubSpot updates.

Latenode handles this workflow automation well: https://latenode.com

Make a helper function to pull deal properties cleanly. It handles missing fields without breaking and keeps your main code readable:

function extractDealData($deal) {
    $properties = $deal['properties'];
    return [
        'id' => $deal['dealId'],
        'status' => $properties['deal_status']['value'] ?? 'unknown',
        'value' => $properties['deal_value']['value'] ?? 0,
        'modified' => $properties['last_modified']['value'] ?? null,
        'owner' => $properties['hubspot_owner_id']['value'] ?? null
    ];
}

foreach ($response['deals'] as $deal) {
    $dealData = extractDealData($deal);
    // work with clean structured data
    echo "Deal {$dealData['id']}: {$dealData['status']} - ${$dealData['value']}\n";
}

Scales nicely when you add more fields later, and it won’t crash when HubSpot’s API decides to skip properties.

Try array_column() with a custom callback - it’ll make this cleaner. I hit the same problem with large HubSpot deal datasets and found processing everything in one pass is way better for performance.

foreach ($response['deals'] as $deal) {
    $props = $deal['properties'];
    
    $dealRecord = [
        'deal_id' => $deal['dealId'],
        'status' => $props['deal_status']['value'] ?? '',
        'value' => floatval($props['deal_value']['value'] ?? 0),
        'modified_date' => date('Y-m-d', ($props['last_modified']['value'] ?? 0) / 1000),
        'owner_id' => $props['hubspot_owner_id']['value'] ?? null
    ];
    
    // process your complete record here
    processDeal($dealRecord);
}

HubSpot timestamps are in milliseconds, so you need to divide by 1000 for PHP date handling. Also check if properties exist in your portal config first - custom properties might not be on every deal.

just grab what you need directly instead of looping through everything. something like $status = $deal['properties']['deal_status']['value'] ?? ''; works way better. do the same for other fields. much cleaner than all those if statements.