Last active
December 19, 2018 16:45
-
-
Save kostiantyn-petlia/8f87071158a49d19d8e77c5e5c4c4459 to your computer and use it in GitHub Desktop.
Save JSON parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Save JSON parsing | |
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) { | |
console.log('tryParseJSON() catch!'); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment