This seems to be the issue:
An input is received, via ajax websocket etc, and it is always gonna be in String format - but you need to know if it is JSON.parsable. Touble is, that if you always run it through a JSON.parse, the program MAY continue 'successfully' but you'll still see an error thrown in the console with the dreaded "Error: unexpected token 'x'".
Here's what I tend to do (but its very kludgy):
var data = (data[0] === '{' || data[0] === '[') ? JSON.parse(data) : data; Any insight on a sure catch-all way without errors?
ps. this is kludgy, don't up-vote.
EDIT:
After months of dealing with this issue here and there, it seems the best approach is to use try/catch() and use your own error handling (even if initially its just a console log et cetera):
try { JSON.parse("a"); // Produces a SyntaxError } catch (error) { // Handle the error console.log(error.message); } Unfortunately, there's not really elegant degradation for this that I'm aware of.