Home > JavaScript > Invalid JSON if integer value starts with 0

Invalid JSON if integer value starts with 0

If any integer value in the JSON starts with 0, the parser will throw an error as “invalid key:value pair” or “SyntaxError: Unexpected number in JSON at position xx” . The reason of this error is because if the integer begins with leading 0 then it will consider this value as Octal number. As Octal number always starts with 0 in javascript.

And it seems that JSON does not support the Octal number, so giving this error. So, the take away is this if in any chance your code requirement is to pass any numbers leading with 0 in JSON, better change the data type of that key from integer to string.

For example the below JSON will give you error, because the integer value in the employee id column starts with 0.

{
    "employeeName" : "Jkoder",
    "employeeId" : 099874364
}

SyntaxError: Unexpected number in JSON at position 42

But if the JSON is changed to the below one, then there won’t be any error with the JSON.

{
    "employeeName" : "Jkoder",
    "employeeId" : "099874364"
}