I have a requirement where I get json string from other API, and this string may or may not be a valid JSON string. How do I Check if the JSON String is valid or not, if it's not valid, how can I make it valid ? What I mean to ask is if there are certain characters that need to be escaped and if they are not escaped then we get error while parsing. Do we have any API to make a JSON string valid ?
I got the code to check if a JSON string is valid or not
Javascript
if (/^[\],:{}\s]*$/.test('{section : "ABCDEFGHI"JKLMNOP"}'.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
console.log('valid');
}else{
console.log('Not a valid');
}