Architect Script Node failing to parse JSON from BYOC Trunk Webhook

The Script node in Architect is throwing a SyntaxError: Unexpected token when attempting to JSON.parse the payload from our APAC BYOC trunk webhook. The raw body arrives correctly via the HTTP request node, but the script execution halts immediately upon parsing. Has anyone encountered serialization issues with specific carrier headers in the webhook payload?

This error typically stems from non-JSON headers appended to the body, not carrier issues. In Architect, ensure the HTTP Request node is configured to return only the response body. Alternatively, validate the payload string length and trim whitespace before parsing. The Performance dashboard often flags these as script timeouts due to execution halts.

It looks like the payload might be carrying extra metadata or BOM characters that JSON.parse chokes on immediately. When dealing with BYOC webhooks, the raw body often includes carriage returns or null bytes that aren’t visible in the Architect debug view but break strict parsing.

Try adding a pre-processing step in the Script node to sanitize the string before attempting to parse. A quick regex replacement usually clears these hidden characters:

let cleanPayload = body.replace(/[\u0000-\u001F]+/g, '');
try {
 let data = JSON.parse(cleanPayload);
 flow.setVariable("parsedData", data);
} catch (e) {
 // Log the error for debugging
}

This approach saved us when integrating with a similar carrier webhook that appended invisible control characters. It ensures the parser only sees valid JSON structure. Double-check the HTTP node settings too, as sometimes the content-type header mismatch causes the body to be encoded unexpectedly.