I’m dealing with a Mailgun webhook response and trying to retrieve specific header information from it. This webhook sends data when an email is successfully delivered.
The issue is that the JSON structure is quite unusual to me. It doesn’t follow a standard object format with easy key-value pairs. Instead, I have this nested array structure, which complicates the extraction of the information I need.
I specifically want to extract the subject line, but I’m uncertain about the best method to do this. Currently, I’m considering using several foreach loops, but that approach seems quite messy.
let webhookData = [
["Received-By", "processed by hermes.mailgun.net via SMTP handler 9847562188944; Mon, 15 Jun 2023 14:32:18 +0000"],
["Content-Type", ["multipart/mixed", {"boundary": "abc123def456ghi789jkl012"}]],
["MIME-Version", "1.0"],
["Subject", "Welcome to our service"],
["From", "Support Team <[email protected]>"],
["To", "User <[email protected]>"],
["Message-ID", "<[email protected]>"],
["X-Mailgun-Vars", "{\"campaign_id\": \"summer_2023\", \"user_type\": \"premium\"}"],
["Date", "Mon, 15 Jun 2023 14:32:18 +0000"],
["Sender", "[email protected]"]
];
Is there a more efficient way to extract header values from this format?
This nested array format is pretty common with email headers. You don’t need multiple foreach loops.
Just convert it to a Map or use find():
// Option 1: Convert to Map for multiple lookups
const headersMap = new Map(webhookData);
const subject = headersMap.get('Subject');
const from = headersMap.get('From');
// Option 2: Single extraction with find()
const subject = webhookData.find(header => header[0] === 'Subject')?.[1];
Both approaches are clean and skip the messy loops.
Here’s the thing though - if you’re processing Mailgun webhooks regularly, you’re probably doing way more than just extracting headers. You might need to log data, update databases, send notifications, or trigger other workflows.
I’ve dealt with similar webhook processing at scale. Manual parsing gets old fast. What really works is setting up proper automation that handles the entire webhook pipeline - parsing, validation, routing to different actions based on event types, error handling, retries.
Latenode makes this straightforward. You can create a workflow that automatically parses Mailgun webhooks, extracts whatever headers you need, and connects them to your other systems without writing parsing code over and over.
The array structure you’re seeing is just how email headers get serialized. Mailgun uses arrays to preserve order and handle duplicate headers.
All these parsing approaches work fine for extracting data. But after dealing with tons of webhook endpoints, I’ve learned parsing is actually the easy part.
The real challenge comes after you extract that subject line. You’ll need to validate webhook signatures, check for duplicate events, update user records, maybe trigger follow-up emails. Then there’s error handling, logging, retries when services go down.
I wasted tons of time building custom webhook processors that became maintenance nightmares. Same parsing logic everywhere, different error patterns, inconsistent logging.
Now I route all webhook processing through automation flows. Takes the payload, handles parsing automatically, validates signatures, and connects to whatever systems need the data. No more custom functions or maintenance headaches.
Latenode handles Mailgun webhooks natively and extracts any header you need without writing parsing code. Plus it handles all the operational stuff like retries and error handling.
Destructuring works perfectly here. Try const subject = webhookData.find(([key]) => key === 'Subject')?.[1]; - it’ll grab what you need without any extra overhead. I grab specific headers this way since you rarely need everything anyway.
that nested array structure is kinda weird but it’s just headers in array format. use Object.fromEntries(webhookData) to convert it to a normal object, then access with obj.Subject or obj['Subject']. much simpler than manually looping through everything.
Been working with Mailgun webhooks for years and see this structure all the time. The array format’s actually pretty smart - it keeps header order intact and handles duplicates properly.
The other suggestions work, but I just use a simple reduce when I need multiple headers:
Gives you exactly what you need without building a full Map or Object from everything. Super useful with big webhook payloads when you only want a few values. Won’t break if headers are missing either.
That’s just how email headers show up as arrays of key-value pairs. I’ve dealt with similar webhook setups before - there’s a pretty clean way to handle it.
Just use find() to grab specific headers without converting everything:
This works great because it stops looking once it finds what you need. Way better than converting everything when you only want a couple values.
Watch out for headers like Content-Type though - they can get messy with nested arrays and objects. You might need extra handling depending on what you’re pulling out.