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?
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?
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.
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.
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).