Skip to content

Instantly share code, notes, and snippets.

@ravishakya
Created September 19, 2016 10:52
Show Gist options
  • Save ravishakya/4f5dda63dbe945e103e0c32ab0e453a2 to your computer and use it in GitHub Desktop.
Save ravishakya/4f5dda63dbe945e103e0c32ab0e453a2 to your computer and use it in GitHub Desktop.
function tryParseJSON(jsonString){
try {
var o = JSON.parse(jsonString);
// Handle non-exception-throwing cases:
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
// but... JSON.parse(null) returns null, and typeof null === "object",
// so we must check for that, too. Thankfully, null is falsey, so this suffices:
if (o && typeof o === "object") {
return o;
}
}
catch (e) { }
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment