How to check if a string is a valid JSON string in JavaScript without using Try/Catch


Something like:

var jsonString = '{ "Id": 1, "Name": "Coke" }';

//should be true

//should be false

The solution should not contain try/catch. Some of us turn on "break on all errors" and they don't like the debugger breaking on those invalid JSON strings.

3/18/2016 8:36:28 PM

Accepted Answer

A comment first. The question was about not using try/catch.
If you do not mind to use it, read the answer below. Here we just check a JSON string using a regexp, and it will work in most cases, not all cases.

Have a look around the line 450 in

There is a regexp that check for a valid JSON, something like:

if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

  //the json is ok


  //the json is not ok


EDIT: The new version of json2.js makes a more advanced parsing than above, but still based on a regexp replace ( from the comment of @Mrchief )

8/9/2018 1:59:06 PM

I know i'm 3 years late to this question, but I felt like chiming in.

While Gumbo's solution works great, it doesn't handle a few cases where no exception is raised for JSON.parse({something that isn't JSON})

I also prefer to return the parsed JSON at the same time, so the calling code doesn't have to call JSON.parse(jsonString) a second time.

This seems to work well for my needs:

function tryParseJSON (jsonString){
    try {
        var o = JSON.parse(jsonString);

        // Handle non-exception-throwing cases:
        // Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
        // but... JSON.parse(null) returns null, and typeof null === "object", 
        // so we must check for that, too. Thankfully, null is falsey, so this suffices:
        if (o && typeof o === "object") {
            return o;
    catch (e) { }

    return false;

// vanillaJS
function isJSON(str) {
    try {
        return (JSON.parse(str) && !!str);
    } catch (e) {
        return false;

Usage: isJSON({}) will be false, isJSON('{}') will be true.

To check if something is an Array or Object (parsed JSON):

// vanillaJS
function isAO(val) {
    return val instanceof Array || val instanceof Object ? true : false;

// ES2015
var isAO = (val) => val instanceof Array || val instanceof Object ? true : false;

Usage: isAO({}) will be true, isAO('{}') will be false.


Here my working code:

function IsJsonString(str) {
  try {
    var json = JSON.parse(str);
    return (typeof json === 'object');
  } catch (e) {
    return false;

I used a really simple method to check a string how it's a valid JSON or not.

function testJSON(text){
    if (typeof text!=="string"){
        return false;
        return true;
    catch (error){
        return false;

Result with a valid JSON string:

var input='["foo","bar",{"foo":"bar"}]';
testJSON(input); // returns true;

Result with a simple string;

var input='This is not a JSON string.';
testJSON(input); // returns false;

Result with an object:

var input={};
testJSON(input); // returns false;

Result with null input:

var input=null;
testJSON(input); // returns false;

The last one returns false because the type of null variables is object.

This works everytime. :)


In prototypeJS, we have method isJSON. You can try that. Even json might help.

// -> false
// -> true
"{ foo: 42 }".isJSON();
// -> false
"{ \"foo\": 42 }".isJSON();

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]