You can use the following to verify that the string is a valid representation of the JSON object:
function parseJson(str) { try { return JSON.parse(str); } catch (err) { return false; } }
Using:
var parsed = parseJson(someInput); if (parsed === false) {
If you also need to validate the object using some custom logic (for example, โI need your object to have X and Y attributesโ), check out JsonSchema .
source share