Nested loops in Zapier webhook processing

I have a working piece of code that processes data using nested for loops. It performs well when I run it locally, but when I try to use the same logic in my Zapier webhook handler, I encounter errors indicating that certain properties are undefined.

Here’s the structure of my test data:

var data = {
    "endpoint": "api/getData",
    "method": "FETCH",
    "output": {
        "headers": ["Username", "contact", "Created"],
        "records": [
            ["john1", "[email protected]", "15 Jan, 2018 10:25:30"],
            ["mary2", "[email protected]", "15 Jan, 2018 14:20:15"]
        ]
    }
};

var output = [];

for (var x = 0; x < data.output.records.length; x++) {
    var item = {};
    for (var y = 0; y < data.output.headers.length; y++) {
        item['"' + data.output.headers[y] + '"'] = data.output.records[x][y];
    }
    output.push(item);
}

console.log(output);

This generates the output I expect. However, when I modify it for use in Zapier, I receive the error TypeError: Cannot read property 'records' of undefined. Here’s how my Zapier function looks:

process_webhook: function(bundle) {
    var data = z.JSON.parse(bundle.response.content);
    var output = [];
    
    for (var x = 0; x < data.output.records.length; x++) {
        var item = {};
        for (var y = 0; y < data.output.headers.length; y++) {
            item['"' + data.output.headers[y] + '"'] = data.records[x][y];
        }
        output.push(item);
    }
    return output || [];
}

What might be causing this discrepancy in performance between my local testing and the Zapier environment?

zapier’s webhook handling can be a pain. wrap your loops in try-catch blocks and add null checks before you access nested properties. the webhook payload gets mangled sometimes or doesn’t match what you expect. also check that bundle.response.content is actually valid JSON - i’ve seen it come back as a string that looks like JSON but isn’t formatted right.

Found your bug - there’s a typo in your Zapier nested loop. Your local version correctly uses data.output.records[x][y], but the Zapier function has data.records[x][y] (missing the .output part). That’s why you’re getting the undefined property error - data.records doesn’t exist in your structure. Also throw in some defensive coding to check if data.output exists before accessing it. I’ve hit similar issues when webhook payloads change slightly from what you expect during testing. Your JSON parsing looks fine, so that typo’s probably the main issue.

The webhook response structure is probably different from your test data. Your test data has the output property with headers and records, but the actual webhook might structure things differently. I’d add some debugging to see what’s actually coming through. Try logging JSON.stringify(data) right after parsing to check the real structure. Webhooks often return nested data or wrap responses in extra layers, so you might need to adjust your data path based on what actually gets sent. The parsing looks right, but webhook responses rarely match test data exactly in my experience.