Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON standard - floating point numbers

I am wondering whether the following floating point notation is a valid JSON notation:

"result":{"base_fee":1e-005}

or should the exponent notation be replaced with a decimal notation?

like image 482
ThePiachu Avatar asked Oct 24 '13 00:10

ThePiachu


People also ask

Does JSON support floating-point 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.

How are numbers represented in JSON?

JSON numbers follow JavaScript's double-precision floating-point format. Represented in base 10 with no superfluous leading zeros (e.g. 67, 1, 100). Include digits between 0 and 9. Can be a negative number (e.g. -10 .

Should numbers be quoted in JSON?

In JSON, 6 is the number six. "6" is a string containing the digit 6 . So the answer to the question "Can json numbers be quoted?" is basically "no," because if you put them in quotes, they're not numbers anymore.

What are valid values in JSON?

Valid Data Typesa number. an object (JSON object) an array. a boolean.


3 Answers

It is valid according to the format available at json.org as numbers can optionally have a base 10 exponent denoted by an E, uppercase or lowercase, an optional plus or minus, and one or more digits.

image of JSON number format

like image 169
Delan Azabani Avatar answered Oct 22 '22 10:10

Delan Azabani


While from a JSON (and JavaScript) perspective these four numerals

a) 100
b) 100.0
c) 1.0E+2
d) 1E+2

are just four ways to write the exact same number, in environments where integers and reals are distinct types of numbers they might not all be equivalent.

And while (a) clearly means an integer, and (b) a real, and (c) a real as well, the case (d) is a bit ambiguous: for example, in C this is a floating point literal (because there's an exponent), but in Ada it is an integer literal (because there's no decimal point).

And in ISO 6093:1985 "Information processing – Representation of numerical values in character strings for information interchange", the last one is invalid, while the other three correspond to the three distinguishable formats NR1, NR2, and NR3 defined there.

So in general—in JSON or elsewhere—, I would prefer and recommend to always include a decimal point in a "scientific" decimal string representation with an exponent.

And to place at least one digit in front of the decimal point (if there is one), as JSON (and Ada, but not C) requires and ISO 6093 recommends (but not requires).

Just to avoid misunderstandings (among humans) or data exchange hassles (among machines and programs).

like image 24
mrtnhfmnn Avatar answered Oct 22 '22 08:10

mrtnhfmnn


It's perfectly valid, according to RFC 4627 RFC 7159*:

The representation of numbers is similar to that used in most programming languages. A number contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part.

Octal and hex forms are not allowed. Leading zeros are not allowed.

A fraction part is a decimal point followed by one or more digits.

An exponent part begins with the letter E in upper or lowercase, which may be followed by a plus or minus sign. The E and optional sign are followed by one or more digits.

Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted.

Exponents are permitted to have leading 0s, but not the integer section:

number = [ minus ] int [ frac ] [ exp ]

decimal-point = %x2E       ; .

digit1-9 = %x31-39         ; 1-9

e = %x65 / %x45            ; e E

exp = e [ minus / plus ] 1*DIGIT

frac = decimal-point 1*DIGIT

int = zero / ( digit1-9 *DIGIT )

minus = %x2D               ; -

plus = %x2B                ; +

zero = %x30                ; 0

* The RFC 7159 standard supercedes the RFC 4627 informational memo, however the grammar used remains exactly the same.

like image 15
Qantas 94 Heavy Avatar answered Oct 22 '22 09:10

Qantas 94 Heavy