javascript check if string is json parsable
var isJsonParsable = string => {
try {
JSON.parse(string);
} catch (e) {
return false;
}
return true;
}function isJson(str) {
try {
return JSON.parse(str);
} catch (e) {
return false;
}
}function isValidJSONString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
//usage
var personJSONString = '{"first_name":"Tony","last_name":"Hawk","age":31}';
if(isValidJSONString(personJSONString)){
//cool we are valid, lets parse
var person= JSON.parse(personJSONString);
}
Also in JavaScript:
- angular prod apache config for route
- How to hthe amount of users online in discordjs
- parseint array javascript
- find match in array object js
- js is undefined or null
- how to turn decimales into percents with javascript
- count word and space in text javascript
- javascript convert int to float with 2 decimal places
- check checkbox by jquery
- how to get url node
- javacript getHTTPURL
- react native function
- how to remove duplicate array object in javascript
- laravel return response json
- jest expect not contain
- Setting axios base url dynamically
- loop through an array javascript
- firebase realtime database query where number bigger
- Javascript object to JSON string
- route in component react
- javascript picture delete after time
- javascript add new array element to start of array
- javascript detect click outside element
- contains substring javascript