"List Events" endpoint returns invalid JSON in the payload

When I issue a GET request to /zoom_events/events I get a HTTP 401 response with the following payload:

{
  "code":104, 
  "message":"Invalid access token, does not contain scopes:["zoom_events_basic:read","zoom_events_basic:read:admin"]"
}

When I get such a response, I parse the JSON in the payload to get the detailed error message and display it to the end user. Unfortunately, the JSON in the payload is not valid due to the fact that double-quotes are used in the content of the message property. Specifically around zoom_events_basic:read and zoom_events_basic:read:admin. The double quotes around each scope should be encoded like so: \".

Any chance the API could be fixed to ensure the returned JSON string is valid?

Thanks

@ojus.zoom

Hi @desautelsj ,

I have created a request internally to fix this issue (ZOOM-789295), and while we are trying to fix it, here is an example logic in js on how you can handle this issue at your end:

function parseAndFixApiResponse(response) {
    try {
        // Attempt to parse the JSON response
        return JSON.parse(response);
    } catch (error) {
        // If parsing fails, try to fix the invalid JSON
        let correctedResponse = response;

        // Find the "message" property in the JSON string
        const messageMatch = correctedResponse.match(/"message":"([^"]*)"/);
        if (messageMatch) {
            let message = messageMatch[1];

            // Replace unescaped double quotes within the message content
            message = message.replace(/:\["([^"]*)","([^"]*)"\]/g, ':["$1","$2"]');
            message = message.replace(/"/g, '\\"'); // Escape any remaining unescaped double quotes

            // Replace the original message in the JSON string with the corrected one
            correctedResponse = correctedResponse.replace(messageMatch[1], message);
        }

        // Parse and return the corrected JSON
        return JSON.parse(correctedResponse);
    }
}

// Example usage:
const response = `{
  "code": 104,
  "message": "Invalid access token, does not contain scopes:[\\"zoom_events_basic:read\\",\\"zoom_events_basic:read:admin\\"]"
}`;

try {
    const parsedResponse = parseAndFixApiResponse(response);
    console.log(parsedResponse);
    // Output or display the message to the end user as needed
} catch (e) {
    console.error("Failed to parse the response:", e);
}

@ojus.zoom This is also happening on the “get scheduled events” endpoint for Scheduler API if the event has guests. The guests are being returned as malformed JSON.