diff --git a/app/client/src/pages/Editor/QueryEditor/QueryDebuggerTabs.tsx b/app/client/src/pages/Editor/QueryEditor/QueryDebuggerTabs.tsx index b3f7578d0c6f..6b6625159c82 100644 --- a/app/client/src/pages/Editor/QueryEditor/QueryDebuggerTabs.tsx +++ b/app/client/src/pages/Editor/QueryEditor/QueryDebuggerTabs.tsx @@ -51,6 +51,27 @@ interface QueryDebuggerTabsProps { onRunClick: () => void; showSchema?: boolean; } +const parseResponseBody = (body: any): any[] => { + let parsedOutput: any; + + if (typeof body === "string") { + try { + parsedOutput = JSON.parse(body); + } catch (e) { + return [{ response: body }]; + } + } else { + parsedOutput = body; + } + + if (Array.isArray(parsedOutput)) { + return parsedOutput; + } + if (parsedOutput.records && Array.isArray(parsedOutput.records)) { + return parsedOutput.records; + } + return [parsedOutput]; +}; function QueryDebuggerTabs({ actionName, @@ -154,7 +175,7 @@ function QueryDebuggerTabs({ if (isString(actionResponse.body)) { try { // Try to parse response as JSON array to be displayed in the Response tab - output = JSON.parse(actionResponse.body); + output = parseResponseBody(actionResponse.body); } catch (e) { // In case the string is not a JSON, wrap it in a response object output = [ @@ -163,10 +184,9 @@ function QueryDebuggerTabs({ }, ]; } - } else { - // TODO: Fix this the next time the file is edited - // eslint-disable-next-line @typescript-eslint/no-explicit-any - output = actionResponse.body as any; + } else { + output = parseResponseBody(actionResponse.body); + } }