1var isJsonParsable = string => {
2 try {
3 JSON.parse(string);
4 } catch (e) {
5 return false;
6 }
7 return true;
8}
1function isJson(str) {
2 try {
3 return JSON.parse(str);
4 } catch (e) {
5 return false;
6 }
7 }
1function isValidJSONString(str) {
2 try {
3 JSON.parse(str);
4 } catch (e) {
5 return false;
6 }
7 return true;
8}
9//usage
10var personJSONString = '{"first_name":"Tony","last_name":"Hawk","age":31}';
11if(isValidJSONString(personJSONString)){
12 //cool we are valid, lets parse
13 var person= JSON.parse(personJSONString);
14}
15