I want to alter the behavior of JSON.parse is there anyway to do this in JS?
I need to do this because we are encountering some errors when trying to parse as json some data that the backend is returning as html.
I want to alter the behavior of JSON.parse is there anyway to do this in JS?
I need to do this because we are encountering some errors when trying to parse as json some data that the backend is returning as html.
Yes, you can by overriding the method:
JSON.parse = function(str) {
console.log(str);
}
JSON.parse([1, 2]);
Why not just use try-catch to handle the response recieved from the backend as described here
How to check if a string is a valid JSON string in JavaScript without using Try/Catch
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}