Answers for "how to check if string is json parsable"

3

javascript check if string is json parsable

var isJsonParsable = string => {
    try {
        JSON.parse(string);
    } catch (e) {
        return false;
    }
    return true;
}
Posted by: Guest on April-28-2020
0

check if string is json parsable

if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

  //the json is ok

}else{

  //the json is not ok

}
Posted by: Guest on August-30-2021

Code answers related to "how to check if string is json parsable"

Code answers related to "Javascript"

Browse Popular Code Answers by Language