Update isValidJSON.md

This commit is contained in:
Angelos Chalaris
2017-12-31 15:12:16 +02:00
committed by GitHub
parent e59829b9ef
commit 551c9b9a21

View File

@ -2,15 +2,13 @@
Checks if the provided argument is a valid JSON. 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. Use `JSON.parse()` and a `try... catch` block to check if the provided argument is a valid JSON and non-null.
```js ```js
const isValidJSON = obj => { const isValidJSON = obj => {
try { try {
JSON.parse(obj); JSON.parse(obj);
if (o && typeof o === "object") { return !!obj;
return true;
}
} catch (e) { } catch (e) {
return false; return false;
} }