I know this is a very old question with good answers. However, it seems that it's still possible to add my 2ยข to it.
Assuming that you're trying to test not a JSON object itself but a String that is formatted as a JSON (which seems to be the case in your var data
), you could use the following function that returns a boolean (is or is not a 'JSON'):
function isJsonString( jsonString ) {
// This function below ('printError') can be used to print details about the error, if any.
// Please, refer to the original article (see the end of this post)
// for more details. I suppressed details to keep the code clean.
//
let printError = function(error, explicit) {
console.log(`[${explicit ? 'EXPLICIT' : 'INEXPLICIT'}] ${error.name}: ${error.message}`);
}
try {
JSON.parse( jsonString );
return true; // It's a valid JSON format
} catch (e) {
return false; // It's not a valid JSON format
}
}
Here are some examples of using the function above:
console.log('\n1 -----------------');
let j = "abc";
console.log( j, isJsonString(j) );
console.log('\n2 -----------------');
j = `{"abc": "def"}`;
console.log( j, isJsonString(j) );
console.log('\n3 -----------------');
j = '{"abc": "def}';
console.log( j, isJsonString(j) );
console.log('\n4 -----------------');
j = '{}';
console.log( j, isJsonString(j) );
console.log('\n5 -----------------');
j = '[{}]';
console.log( j, isJsonString(j) );
console.log('\n6 -----------------');
j = '[{},]';
console.log( j, isJsonString(j) );
console.log('\n7 -----------------');
j = '[{"a":1, "b": 2}, {"c":3}]';
console.log( j, isJsonString(j) );
When you run the code above, you will get the following results:
1 -----------------
abc false
2 -----------------
{"abc": "def"} true
3 -----------------
{"abc": "def} false
4 -----------------
{} true
5 -----------------
[{}] true
6 -----------------
[{},] false
7 -----------------
[{"a":1, "b": 2}, {"c":3}] true
Please, try the snippet below and let us know if this works for you. :)
IMPORTANT: the function presented in this post was adapted from https://airbrake.io/blog/javascript-error-handling/syntaxerror-json-parse-bad-parsing where you can find more and interesting details about the JSON.parse() function.
function isJsonString( jsonString ) {_x000D_
_x000D_
let printError = function(error, explicit) {_x000D_
console.log(`[${explicit ? 'EXPLICIT' : 'INEXPLICIT'}] ${error.name}: ${error.message}`);_x000D_
}_x000D_
_x000D_
_x000D_
try {_x000D_
JSON.parse( jsonString );_x000D_
return true; // It's a valid JSON format_x000D_
} catch (e) {_x000D_
return false; // It's not a valid JSON format_x000D_
}_x000D_
_x000D_
}_x000D_
_x000D_
_x000D_
console.log('\n1 -----------------');_x000D_
let j = "abc";_x000D_
console.log( j, isJsonString(j) );_x000D_
_x000D_
console.log('\n2 -----------------');_x000D_
j = `{"abc": "def"}`;_x000D_
console.log( j, isJsonString(j) );_x000D_
_x000D_
console.log('\n3 -----------------');_x000D_
j = '{"abc": "def}';_x000D_
console.log( j, isJsonString(j) );_x000D_
_x000D_
console.log('\n4 -----------------');_x000D_
j = '{}';_x000D_
console.log( j, isJsonString(j) );_x000D_
_x000D_
console.log('\n5 -----------------');_x000D_
j = '[{}]';_x000D_
console.log( j, isJsonString(j) );_x000D_
_x000D_
console.log('\n6 -----------------');_x000D_
j = '[{},]';_x000D_
console.log( j, isJsonString(j) );_x000D_
_x000D_
console.log('\n7 -----------------');_x000D_
j = '[{"a":1, "b": 2}, {"c":3}]';_x000D_
console.log( j, isJsonString(j) );
_x000D_