Skip to content

Instantly share code, notes, and snippets.

@NoMan2000
Last active August 29, 2015 14:11
Show Gist options
  • Save NoMan2000/dd4b7d6a5fc9ac82d0ae to your computer and use it in GitHub Desktop.
Save NoMan2000/dd4b7d6a5fc9ac82d0ae to your computer and use it in GitHub Desktop.
var isValidJSON = function isValidJSON(data) {
if (String(data).length === 0) {
return false;
}
try {
var o = JSON.parse(data);
// if o checks for null, empty, or undefined.
// typeof object and not instanceof array makes sure it's an object and not an array.
// the error checker does the rest.
if (o && typeof o === "object" && !(o instanceof Array)) {
return true;
}
} catch (e) {
return false;
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment