1

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 the 0's easily enough, but I need the values formatted that way for display purposes, is there a simple way for me to parse this into an array of strings instead of an array of numbers?

Also, what would be the simplest way to remove all preceding 0's in the string? I am assuming a regex would be best? But any other suggestions are welcome.

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

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

De nada, q bueno q te haya servido ;)
Saludos, desde La Paz, Bolivia
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.