Skip to content

Commit

Permalink
fix: improve JSON parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Pragadesh-45 committed Dec 30, 2024
1 parent 1f2bee1 commit 7b935bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/bruno-cli/src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ const parseDataFromResponse = (response, disableParsingResponseJson = false) =>
// Filter out ZWNBSP character
// https://gist.github.com/antic183/619f42b559b78028d1fe9e7ae8a1352d
data = data.replace(/^\uFEFF/, '');
if (!disableParsingResponseJson) {

// If the response is a string and starts and ends with double quotes, it's a stringified JSON and should not be parsed
if (!disableParsingResponseJson && !(typeof data === 'string' && data.startsWith('"') && data.endsWith('"'))) {
data = JSON.parse(data);
}
} catch { }
} catch {
console.log('Failed to parse response data as JSON');
}

return { data, dataBuffer };
};
Expand Down
2 changes: 1 addition & 1 deletion packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ const parseDataFromResponse = (response, disableParsingResponseJson = false) =>
try {
// Filter out ZWNBSP character
// https://gist.github.com/antic183/619f42b559b78028d1fe9e7ae8a1352d
data = data.replace(/^\uFEFF/, '');

// If the response is a string and starts and ends with double quotes, it's a stringified JSON and should not be parsed
data = data.replace(/^\uFEFF/, '');
if ( !disableParsingResponseJson && ! (typeof data === 'string' && data.startsWith("\"") && data.endsWith("\""))) {
data = JSON.parse(data);
}
Expand Down

0 comments on commit 7b935bd

Please sign in to comment.