Skip to content

Instantly share code, notes, and snippets.

@nancystodd
Created July 22, 2020 16:53
Show Gist options
  • Save nancystodd/117a2dae0b73f2fb25b63f3d16a4d11d to your computer and use it in GitHub Desktop.
Save nancystodd/117a2dae0b73f2fb25b63f3d16a4d11d to your computer and use it in GitHub Desktop.
function (someString) {
// test string is opened with curly brace or machine bracket
if (someString.trim().search(/^(\[|\{){1}/) > -1) {
try { // it is, so now let's see if its valid JSON
var myJson = JSON.parse(someString);
// yep, we're working with valid JSON
} catch (e) {
// nope, we got what we thought was JSON, it isn't; let's handle it.
}
} else {
// nope, we're working with non-json, no need to parse it fully
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment