diff --git a/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js b/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js index 4ef6b1cfd..605bcd362 100644 --- a/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js +++ b/packages/bruno-app/src/components/ResponsePane/QueryResult/index.js @@ -9,7 +9,7 @@ import QueryResultPreview from './QueryResultPreview'; import StyledWrapper from './StyledWrapper'; import { useState, useMemo, useEffect } from 'react'; import { useTheme } from 'providers/Theme/index'; -import { uuid } from 'utils/common/index'; +import { prettifyJson, uuid } from 'utils/common/index'; const formatResponse = (data, dataBuffer, mode, filter) => { if (data === undefined || !dataBuffer) { @@ -33,12 +33,15 @@ const formatResponse = (data, dataBuffer, mode, filter) => { if (filter) { try { data = JSONPath({ path: filter, json: data }); + return prettifyJson(JSON.stringify(data)); } catch (e) { console.warn('Could not apply JSONPath filter:', e.message); } } - return safeStringifyJSON(data, true); + // Prettify the JSON string directly instead of parse->stringify to avoid + // issues like rounding numbers bigger than Number.MAX_SAFE_INTEGER etc. + return prettifyJson(rawData); } if (mode.includes('xml')) { @@ -53,7 +56,7 @@ const formatResponse = (data, dataBuffer, mode, filter) => { return data; } - return safeStringifyJSON(data, true); + return prettifyJson(rawData); }; const formatErrorMessage = (error) => { diff --git a/packages/bruno-app/src/utils/common/index.js b/packages/bruno-app/src/utils/common/index.js index f9f1c7fbe..bf514f032 100644 --- a/packages/bruno-app/src/utils/common/index.js +++ b/packages/bruno-app/src/utils/common/index.js @@ -1,5 +1,6 @@ import { customAlphabet } from 'nanoid'; import xmlFormat from 'xml-formatter'; +import { format as jsoncFormat, applyEdits as jsoncApplyEdits } from 'jsonc-parser'; // a customized version of nanoid without using _ and - export const uuid = () => { @@ -26,6 +27,13 @@ export const waitForNextTick = () => { }); }; +export const prettifyJson = (doc) => { + return jsoncApplyEdits( + doc, + jsoncFormat(doc, null, {insertSpaces: true, tabSize: 2}) + ); +} + export const safeParseJSON = (str) => { if (!str || !str.length || typeof str !== 'string') { return str;