-3

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.

JJJ
  • 32,902
  • 20
  • 89
  • 102
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186

2 Answers2

1

Yes, you can by overriding the method:

JSON.parse = function(str) {
  console.log(str);
}
JSON.parse([1, 2]);
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
-1

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;
}