The grammar structure of JSON is less complex than JavaScript object literals. Because JSON is designed for data exchange, serialization and deserialization would be frequently. A simple scheme and exclusion of language specific features is the way to go.
Those types are allowed in JSON only:
- String
- Number
- Object
- Array
- Boolean:
true,false null
Thus, literals like undefined and Infinity are all invalid.
Comments are not supported. Neither // nor /* */.
The keys are strings. The rules apply to String also apply to the keys.
Dangling tail comma is not allowed, thus:
{
a: 1,
b: 1,
}The final comma is invalid.
Strings must be wrapped by doubled quotes "; Strings starting with single quotes ' and grave accent ```(string interpolation) are invalid.
-
Format
JSON values human-readability. So it is agnostic about number formats.
- Extra positive sign in JSON is not invalid, e.g.
+1. - Redudant
0s in head is considered invalid, thus001,00.1are all invalid in JSON; - Numbers must be decimal system; not hexadecimal or others, thus,
0x01is in valid in JSON;
- Extra positive sign in JSON is not invalid, e.g.
-
Precision
JSON spec has no limit in number precisions, while JavaScript does.
Conversion from JSON to JavaScript may cause precision loss in number.