I am checking if the input value is json type or not using JavaScript but for some particular value its failing. I am explaining my code below.
isJSON = async(str) => {
try {
return (JSON.parse(str) && !!str);
} catch (e) {
return false;
}
}
var testJSON = '{"name": "foo"}'
var number = 22
console.log("result is : ", isJSON(testJSON))
console.log("result is : ", isJSON(number))
Here str
contains the input value and this method is checking that value is json
type or not. But if the input value is any number i.e-str=22
then also its returning true. Here I need to check only the input value is json
type value or not. Please help me to resolve the issue.