Create yourself a helper function and use it.
function parseValue(value) {
try
{
return JSON.parse(value);
}
catch (ex)
{
}
return value;
}
If you're brave enough, you can also extend String.prototype, so calling it will become really direct.
String.prototype.parseJSON = String.prototype.parseJSON || function() {
try
{
return JSON.parse(this);
}
catch (ex)
{
}
return this;
};
And then you just call it this:
var s = "Some non-JSON string";
s.parseJSON();
'{"b":true,"n":1}'.parseJSON();