Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is JSON invalid if an integer begins with a leading zero?

I'm importing some JSON files into my Parse.com project, and I keep getting the error "invalid key:value pair".

It states that there is an unexpected "8".

Here's an example of my JSON:

}  "Manufacturer":"Manufacturer",  "Model":"THIS IS A STRING",  "Description":"",  "ItemNumber":"Number12345",  "UPC":083456789012,  "Cost":"$0.00",  "DealerPrice":" $0.00 ",  "MSRP":" $0.00 ", } 

If I update the JSON by either removing the 0 from "UPC":083456789012, or converting it to "UPC":"083456789012", it becomes valid.

Can JSON really not accept an integer that begins with 0, or is there a way around the problem?

like image 785
DJSrA Avatar asked Dec 08 '14 15:12

DJSrA


People also ask

What makes a JSON file invalid?

It means that the editor failed to get a response to the server or the response wasn't in a valid JSON format. Basically, if the editor can't communicate with the server, it will show this error message instead. To fix the problem, you essentially need to fix whatever is getting in the way of the communication.

Is an integer valid JSON?

The integer type is used for integral numbers. JSON does not have distinct types for integers and floating-point values. Therefore, the presence or absence of a decimal point is not enough to distinguish between integers and non-integers. For example, 1 and 1.0 are two ways to represent the same value in JSON.

What is invalid JSON value?

The Invalid JSON Types scan checks how your service behaves when getting unexpected values in JSON POST requests. Typically, attackers try to throw values of an invalid type to cause the service to reveal the system data through error messages or stack traces.

Can JSON start with number?

So, the answer to the question is still yes, JSON text can start with a square bracket (i.e. an array). But in addition to objects and arrays, it can now also be a number, string or the values false , null or true .


1 Answers

A leading 0 indicates an octal number in JavaScript. An octal number cannot contain an 8; therefore, that number is invalid. Moreover, JSON doesn't (officially) support octal numbers, so formally the JSON is invalid, even if the number would not contain an 8. Some parsers do support it though, which may lead to some confusion. Other parsers will recognize it as an invalid sequence and will throw an error, although the exact explanation they give may differ.

Solution: If you have a number, don't ever store it with leading zeroes. If you have a value that needs to have a leading zero, don't treat it as a number, but as a string. Store it with quotes around it.

In this case, you've got a UPC which needs to be 12 digits long and may contain leading zeroes. I think the best way to store it is as a string.

It is debatable, though. If you treat it as a barcode, seeing the leading 0 as an integral part of it, then string makes sense. Other types of barcodes can even contain alphabetic characters.

On the other hand. A UPC is a number, and the fact that it's left-padded with zeroes to 12 digits could be seen as a display property. Actually, if you left-pad it to 13 digits by adding an extra 0, you've got an EAN code, because EAN is a superset of UPC.

If you have a monetary amount, you might display it as € 7.30, while you store it as 7.3, so it could also make sense to store a product code as a number.

But that decision is up to you. I can only advice you to use a string, which is my personal preference for these codes, and if you choose a number, then you'll have to remove the 0 to make it work.

like image 198
GolezTrol Avatar answered Sep 19 '22 12:09

GolezTrol