From e9321cfb1aa5ed87a5b134e40d800b3c17a782df Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Sun, 31 Dec 2017 18:38:09 +0530 Subject: [PATCH 1/2] Update isValidJSON.md --- snippets/isValidJSON.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/snippets/isValidJSON.md b/snippets/isValidJSON.md index e9898caee..76257a007 100644 --- a/snippets/isValidJSON.md +++ b/snippets/isValidJSON.md @@ -8,8 +8,10 @@ Use `JSON.parse()` and a `try... catch` block to check if the provided argument const isValidJSON = obj => { try { JSON.parse(obj); - return true; - } catch (e) { + if (o && typeof o === "object") { + return true; + } +} catch (e) { return false; } }; From d9e2bd404217ff16f80996e93cebc6801a5a3a1a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 15:12:16 +0200 Subject: [PATCH 2/2] Update isValidJSON.md --- snippets/isValidJSON.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/snippets/isValidJSON.md b/snippets/isValidJSON.md index 76257a007..dadefb897 100644 --- a/snippets/isValidJSON.md +++ b/snippets/isValidJSON.md @@ -2,15 +2,13 @@ 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 const isValidJSON = obj => { try { JSON.parse(obj); - if (o && typeof o === "object") { - return true; - } + return !!obj; } catch (e) { return false; }