Update isValidJSON.md

This commit is contained in:
Angelos Chalaris
2017-12-31 15:17:27 +02:00
committed by GitHub
parent 2096316c55
commit 4774d42eb7

View File

@ -2,13 +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 and non-null. Use `JSON.parse()` and a `try... catch` block to check if the provided argument is a valid JSON.
```js ```js
const isValidJSON = obj => { const isValidJSON = obj => {
try { try {
JSON.parse(obj); JSON.parse(obj);
return !!obj; return true;
} catch (e) { } catch (e) {
return false; return false;
} }
@ -18,4 +18,5 @@ const isValidJSON = obj => {
```js ```js
isValidJSON('{"name":"Adam","age":20}'); // true isValidJSON('{"name":"Adam","age":20}'); // true
isValidJSON('{"name":"Adam",age:"20"}'); // false isValidJSON('{"name":"Adam",age:"20"}'); // false
isValidJSON(null) // true
``` ```