Now apparently \r
, \b
, \t
, \f
, etc aren't the only problematic chars that can give you this error.
Note that some browsers may have additional requirements for the input of JSON.parse
.
Run this test code on your browser:
var arr = [];
for(var x=0; x < 0xffff; ++x){
try{
JSON.parse(String.fromCharCode(0x22, x, 0x22));
}catch(e){
arr.push(x);
}
}
console.log(arr);
Testing on Chrome, I see that it doesn't allow JSON.parse(String.fromCharCode(0x22, x, 0x22));
where x
is 34, 92, or from 0 to 31.
Chars 34 and 92 are the "
and \
characters respectively, and they are usually expected and properly escaped. It's chars 0 to 31 that would give you problems.
To help with debugging, before you do JSON.parse(input)
, first verify that the input doesn't contain problematic characters:
function VerifyInput(input){
for(var x=0; x<input.length; ++x){
let c = input.charCodeAt(x);
if(c >= 0 && c <= 31){
throw 'problematic character found at position ' + x;
}
}
}