Skip to content Skip to sidebar Skip to footer

Syntaxerror: Unexpected Number

I am trying to parse a JSON string comprised of numerical values, but I keep getting SyntaxError: Unexpected number. When I remove the preceding 0's it works, so I can get rid of t

Solution 1:

It's because of the leading zeros. This works fine:

JSON.parse('[26,27,28,29,30,31,1,2,3,4,5,6,7,8,9]');

JSON definition says integers must not have leading zeros

EDIT To remove the leading zeros:

var str = '[26,27,28,29,30,31,01,02,03,04,05,06,07,08,09]';
str = str.replace(/\b0(\d)/g, "$1");
JSON.parse(str);

Cheers

Solution 2:

JSON.parse('["26","27","28","29","30","31","01","02","03","04","05","06","07","08","09"]');

should parse it as an array of strings instead of array of numbers.

Post a Comment for "Syntaxerror: Unexpected Number"