Node.js Lambda crashing on Genesys Cloud webhook payload

I’m trying to parse a Genesys Cloud routing_queue_member_added webhook in a Node.js Lambda. The trigger works, but the handler throws a SyntaxError when accessing the body. I’ve checked the IAM role and it’s fine, so I assume it’s a parsing issue with the JSON structure.

export const handler = async (event) => {
 console.log('Triggered', event);
 const payload = JSON.parse(event.body);
 console.log(payload.data);
 return { statusCode: 200 };
};

The error log says Unexpected token o in JSON at position 1. I dumped the raw event and the body looks like a stringified JSON object, but JSON.parse is failing on the first character. Is the webhook sending something weird? The payload seems to start with {"data":...}. I’ve tried event.body and event.payload but nothing works. The docs aren’t clear on the exact shape of the body field for these specific webhooks.

The event.body in AWS Lambda is a string, but Genesys Cloud webhooks often send application/json with a Content-Type header that might trigger different parsing behavior depending on your API Gateway integration type. More importantly, you’re double-parsing if your Lambda is configured with “Lambda Proxy Integration” and the API Gateway is already parsing it, or you’re hitting a raw string that isn’t valid JSON.

Check the actual type of event.body before parsing. If it’s already an object, JSON.parse will throw. If it’s a string, ensure it’s not empty.

Here’s the safe pattern:

export const handler = async (event) => {
 console.log('Raw Event:', JSON.stringify(event).substring(0, 500)); // Log first 500 chars
 
 let payload;
 
 // Check if body is already parsed (object) or needs parsing (string)
 if (typeof event.body === 'string') {
 try {
 payload = JSON.parse(event.body);
 } catch (e) {
 console.error('Failed to parse body:', e.message);
 return { statusCode: 400, body: 'Invalid JSON' };
 }
 } else {
 payload = event.body;
 }

 // Genesys Cloud webhook structure
 const data = payload.data || payload; // Handle nested or root data
 
 console.log('Parsed Data:', data);
 
 return { statusCode: 200, body: 'OK' };
};

Also, verify the Content-Type in your Genesys Cloud webhook configuration. It should be application/json. If you set it to text/plain or leave it blank, the payload structure might differ slightly or cause issues with certain middleware.