Bridgbro
Bridgbro

Reputation: 269

Can't JSON parse a string that contains leading zeros.

When I pass this string to JSON.parse it doesn't work, but if I remove the leading zeros on "000634" it does. Does anyone know why this is? Thanks in advance.

This does not work:

    var str = '[["first", "last", "XU10060282", "Dept", 000634, "Position", 156689]]';
    var dataArray = JSON.parse(str);
    console.log(dataArray);

This does work:

    var str = '[["first", "last", "XU10060282", "Dept", 1000634, "Position", 156689]]';
    var dataArray = JSON.parse(str);
    console.log(dataArray);

`

Upvotes: 1

Views: 955

Answers (1)

Greg Brown
Greg Brown

Reputation: 3254

If you don't need to treat it as the number 634, wrapping the value in quotes also works:

var str = '[["first", "last", "XU10060282", "Dept", "000634", "Position", 156689]]';

Upvotes: 1

Related Questions