HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Check if a JavaScript string is a valid JSON

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptcheckvalidjsonstring

Problem

When working with serialized data, you might come across some malformed or invalid JSON strings from time to time. While JavaScript doesn't have a built-in validation method for JSON, it has a handy JSON.parse() method that can be used to check if a string is a valid JSON.
Reading through the documentation, you'll find that JSON.parse() throws a SyntaxError if the string is not a valid JSON. We can use this to our advantage by wrapping the JSON.parse() method in a try...catch block to check if the string is valid.

Solution

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

Code Snippets

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

Context

From 30-seconds-of-code: string-is-valid-json

Revisions (0)

No revisions yet.