• Use JSON.parse(data) to validate e.g. received content. Throws a SyntaxError if JSON.parse() can't handle it because it is invalid.

    // syntax error - need try and catch to handle it
    data = '{"blah": 1 ,"foo": "II", "bar":"three}';
    
    try {
       d = JSON.parse(data);
    }  catch(e){
      console.log("instance:",e instanceof SyntaxError); 
      console.log("message:", e.message);
      //console.log("name:", e.name);
      //console.log("filename:", e.fileName);
      //console.log("lineNumber:", e.lineNumber);
     //console.log("columnNumber:", e.columnNumber);
      console.log("stack:",e.stack);
    }
    /* output 
    instance: true
    message: Expecting a valid value, got UNFINISHED STRING
    stack:  at line 1 col 31
    {"blah": 1 ,"foo": "II", "bar":"three}
                                      ^
    */ 
    
About

Avatar for MaBe @MaBe started