Skip to content

Files

Latest commit

author
Travis CI
Dec 31, 2017
92fbafd · Dec 31, 2017

History

History
22 lines (18 loc) · 428 Bytes

isValidJSON.md

File metadata and controls

22 lines (18 loc) · 428 Bytes

isValidJSON

Checks if the provided argument is a valid JSON.

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

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