There are many bad formatted string object, GET from API, old code, etc. Bad format doesn't means it drops error in code, but drops error for input of JSON.parse().
// not " wrapped key syntax
var str = "{ a: 2 }";
console.log( JSON.parse( str ) );
// SyntaxError: JSON.parse: expected property name or '}' at line 1 column 3 of the JSON data
// 'key', or 'value' syntax
var str = " { 'a': 2 } ";
console.log( JSON.parse( str ) );
// SyntaxError: JSON.parse: expected property name or '}' at line 1 column 3 of the JSON data
//syntax OK
var str = '{ "a": 2 }';
console.log( JSON.parse( str ) );
// Object { a: 2 }
There is a solution:
// Convert any-formatted object string to well formatted object string:
var str = "{'a':'1',b:20}";
console.log( eval( "JSON.stringify( " + str + ", 0, 1 )" ) );
/*
"{
"a": "1",
"b": 20
}"
*/
// Convert any-formatted object string to object:
console.log( JSON.parse( eval( "JSON.stringify( " + str + ", 0, 1 )" ) ) );
// Object { a: "1", b: 20 }