August 29 2016

check if a string is JSON in Javascript

Tagged Under :

javascript
In javascript it don’t have function to check if the string is JSON. So we may need to write a function to check the string if is JSON.

The below function is to check the string and it will return true if the string is JSON else it will return false.
function isJSON($data) {
	var is_json = true;
	try {
		var json = $.parseJSON($data);
	} catch(err) {
		is_json = false;
	}
	return is_json;
}

Make a Comment

*

code