I am new to Javascript trying to create a project that uses JSON.parse() function. My code runs successfully however JSON.parse
dint work and didn't return any error on the dev tools console log.
I tried to debug my code with:
console.log("Test log1#######")
console.log(JSON.parse(json));
console.log("Test log2#######")
On the console.log i can only see "Test log1#######"
. but not the "Test log2#######"
which means code fails at line two but without printing any error on Devtools console as it does normally. But I am sure the code is failing due to some JSON validation issue but I can't see the. error anywhere on dev tools.
Please suggest a method to explore the error log of JSON.parse()
thanks
CodePudding user response:
You can do :
try{
JSON.parse(badFormat);
}catch(err){
console.log(err);
}
CodePudding user response:
Most probably your "json" string is undefined
Try using the reviver parameter to see what is coming out of the json string. JSON.parse() spits out the error in the console tab of the browser if it finds the issue with the passed string
JSON.parse('your string', (key, value) => {
console.log(key); // log the current property name, the last is "".
return value; // return the unchanged property value.
});