Mohan pd.
Mohan pd. Author of The Coders Blog. Also a professional fullstack javascript developer working with various tech company and client around world.

How to handle invalid JSON parse error properly.

Mar 05, 2019 · 2 minutes
How to handle invalid JSON parse error properly.

JSON ( JavaScript Object Notation), is widely used format for asynchronous communication between webpage or mobile application to back-end servers. Due to increasing trend in Single Page Application or Mobile Application, popularity of the JSON is extreme.

Why do you get parse error ?

Parse error due to missing character or unsupported character in the string you provide to json parser.

JSON.Parse Errors and its types.

Mainly, you will receive following error if you have error in JSON string & if your error message say debugging is little twisted for you.

SyntaxError: JSON.parse: unterminated string literal 
SyntaxError: JSON.parse: bad control character in string literal 
SyntaxError: JSON.parse: bad character in string literal 
SyntaxError: JSON.parse: bad Unicode escape 
SyntaxError: JSON.parse: bad escape character 
SyntaxError: JSON.parse: unterminated string 
SyntaxError: JSON.parse: no number after minus sign 
SyntaxError: JSON.parse: unexpected non-digit 
SyntaxError: JSON.parse: missing digits after decimal point 
SyntaxError: JSON.parse: unterminated fractional number 
SyntaxError: JSON.parse: missing digits after exponent indicator 
SyntaxError: JSON.parse: missing digits after exponent sign 
SyntaxError: JSON.parse: exponent part is missing a number 
SyntaxError: JSON.parse: unexpected end of data 
SyntaxError: JSON.parse: unexpected keyword 
SyntaxError: JSON.parse: unexpected character 
SyntaxError: JSON.parse: end of data while reading object contents 
SyntaxError: JSON.parse: expected property name or '}' 
SyntaxError: JSON.parse: end of data when ',' or ']' was expected 
SyntaxError: JSON.parse: expected ',' or ']' after array element 
SyntaxError: JSON.parse: end of data when property name was expected 
SyntaxError: JSON.parse: expected double-quoted property name 
SyntaxError: JSON.parse: end of data after property name when ':' was expected 
SyntaxError: JSON.parse: expected ':' after property name in object 
SyntaxError: JSON.parse: end of data after property value in object 
SyntaxError: JSON.parse: expected ',' or '}' after property value in object 
SyntaxError: JSON.parse: expected ',' or '}' after property-value pair in object literal 
SyntaxError: JSON.parse: property names must be double-quoted strings 
SyntaxError: JSON.parse: expected property name or '}' 
SyntaxError: JSON.parse: unexpected character 
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

How to handle it.

There are several way to handle error.

Try catch block is one of them which is as follow:

function IsJsonString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}

Another way is to test using regex expression and then using try catch block to reducer the execution time.

function IsJsonString(jsonData) {
if (typeof jsonData == 'string') {
    if (! /^[\[|\{](\s|.*|\w)*[\]|\}]$/.test(jsonData)) {
        return jsonData;
    }
}

try {
    jsonData = $.parseJSON(jsonData);
} catch (e) {
  console.log(e);
  return false;

  }
}

Regex only method to check JSON validity:

function IsJsonString(jsonData) {

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

  //the json is ok

}else{

  //the json is not ok

}
}