String is valid JSON

JavaScript, Type · Oct 18, 2020

Checks if the provided string is a valid JSON.

const isValidJSON = str => {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    return false;
  }
};
isValidJSON('{"name":"Adam","age":20}'); // true
isValidJSON('{"name":"Adam",age:"20"}'); // false
isValidJSON(null); // true

More like this

  • Collection is empty

    Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.

    JavaScript, Type · Oct 20, 2020

  • Check if value is of type

    Checks if the provided value is of the specified type.

    JavaScript, Type · Oct 20, 2020

  • Value is array-like

    Checks if the provided argument is array-like (i.e. is iterable).

    JavaScript, Type · Oct 20, 2020