Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are decimals without leading zeros valid JSON?

Given the JSON document

{"percentageAmount": .01}

Running it by JSONLint.com results in the error:

Parse error on line 2:
..."percentageAmount": .01}
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

On the other hand, this is valid:

{"percentageAmount": 0.01}

The code is parsed correctly if assigned to a variable as a JavaScript literal, but of course there are many things that are OK for JavaScript variables that aren't JSON spec.

Why is this against JSON spec?

like image 287
Martin Burch Avatar asked Nov 12 '14 23:11

Martin Burch


1 Answers

Nope.

According to the railroad diagram for numbers at JSON.org, numbers with fractional values must have digits before the decimal point:

Diagram showing that numbers with fraction parts must have a digit before the decimal point.

like image 187
maerics Avatar answered Sep 26 '22 00:09

maerics