30 seconds of code

  Back to Home

isValidJSON

typejsonintermediate

Checks if the provided string is a valid JSON.

Use JSON.parse() and a try... catch block to check if the provided string is a valid JSON.

const isValidJSON = str => {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    return false;
  }
};