JSON Object validation in JavaScript
if you wish to validate the object to a certain schema, you could try JSD Validator
Building on the idea of @Quentin, you can just do something like:
function isValidJson(json) {
try {
JSON.parse(json);
return true;
} catch (e) {
return false;
}
}
console.log(isValidJson("{}")); // true
console.log(isValidJson("abc")); // false
This will require json2.js to be deployed in the page in order to ensure cross-browser support for the JSON
Object
.