Section 12.26

JSON

Full name: JavaScript Object Notation.

Definition

JSON (JavaScript Object Notation) is a text format for exchanging structured data between programs. A JSON text holds one value. A value is an object (an unordered collection of name–value pairs, each name a string), an array (an ordered list of values), a string of Unicode §12.48 characters, a number written in decimal, or one of the literals true, false and null. RFC 8259 defines the syntax. JSON defines only syntax: what a value means, and which values are acceptable, each application decides.

The idea

Two programs written in different languages must agree on a record, such as a configuration with a limit of 16. JSON gives them a common text form, {"limit": 16}, that people can read and nearly every programming language can parse.

The grammar leaves several questions open, and parsers answer them differently: a name may appear twice in one object, a number may be larger or more precise than a program can hold, and a number may be infinite or not a number at all. The text is the same for every program, but the values the programs build from it need not be. When a gateway approves a request that a service then carries out, a disagreement between their parsers lets an attacker win approval for one value and action on another. Every interface that uses JSON therefore needs a written parsing policy, enforced by every parser on the path.

How it works

The grammar of RFC 8259.

Spaces, tabs and line breaks between these parts carry no meaning. A text exchanged between systems that do not form one closed group must be encoded in UTF-8 (the 8-bit Unicode Transformation Format). ECMA-404, a standard of Ecma International (formerly the European Computer Manufacturers Association), defines the same grammar.

What the grammar leaves open. RFC 8259 uses three standards key words: SHOULD (a recommendation that may be ignored only for a good reason), MUST (a requirement) and MAY (a permission).

  1. Duplicate names. Names in an object SHOULD be unique. When they are not, RFC 8259 says, “the behavior of software that receives such an object is unpredictable”. Parsers keep the last value, keep the first, report an error, or hand over every pair.

  2. Numbers. An implementation may limit their range and precision. The RFC notes that integers from −(253−1)-(2^{53} - 1) to 253−12^{53} - 1 are interoperable, because every implementation that uses binary64, the IEEE 754 double-precision floating-point format [271], represents them exactly.

  3. Size. An implementation may limit the size of a text, the depth of nesting and the length of strings.

  4. Extensions. A parser MUST accept every valid JSON text and MAY accept other forms, such as comments or NaN.

Two common parsers. Python’s §12.23 json module and JavaScript’s JSON.parse both keep the last value of a duplicate name, without an error. JSON.parse turns every number into binary64 and rejects NaN and Infinity. Python turns a number without fraction or exponent into an exact int, refusing more than 4300 digits, and any other number into a binary64 float, so 1e400, beyond the binary64 range, becomes infinity. By default Python also accepts NaN, Infinity and -Infinity, and writes them for a non-finite float, producing text that is not JSON. Python’s hooks can enforce a policy: object_pairs_hook receives every name–value pair of each object, in order; parse_constant is called for NaN, Infinity and -Infinity; parse_float receives the digits of each number with a fraction or exponent. On output, allow_nan=False makes json.dumps reject a non-finite float.

Worked example

Duplicate names. A request carries the text {"limit":16,"limit":32}. A gateway that keeps the first value sees 16 and approves the request under its rule that the limit must not exceed 20. The service behind it uses Python’s json.loads, keeps the last value, and applies 32. Neither parser breaks the RFC, yet together they let 32>2032 > 20 through. The policy is to reject the text. In Python:

def unique_pairs(pairs):
result = {}
for name, value in pairs:
if name in result:
raise ValueError("duplicate name: " + name)
result[name] = value
return result
json.loads(text, object_pairs_hook=unique_pairs)

The last line raises ValueError: duplicate name: limit.

A large integer. The text {"id": 9007199254740993} holds 253+12^{53} + 1. Python’s json.loads returns the exact int 9007199254740993; JSON.parse returns 9007199254740992. The reason is the binary64 format: its significand, the part that holds the digits, has 53 bits. Every integer up to 253=9,007,199,254,740,9922^{53} = 9{,}007{,}199{,}254{,}740{,}992 fits exactly. From 2532^{53} up to 2542^{54}, the representable numbers are 253+2k2^{53} + 2k for whole numbers kk, so they lie 2 apart. The value 253+12^{53} + 1 lies exactly halfway between 2532^{53}, where k=0k = 0, and 253+22^{53} + 2, where k=1k = 1. The rounding rule of binary64 breaks such a tie toward the neighbor whose last significand bit is 0, here the one with even kk, which is 2532^{53}. Two different identifiers have become one number. The policy is to allow only integers within ±(253−1)=±9,007,199,254,740,991\pm(2^{53} - 1) = \pm 9{,}007{,}199{,}254{,}740{,}991 and to send larger identifiers as strings, such as "9007199254740993".

Non-finite numbers. In Python, json.dumps({"x": float("nan")}) produces {"x": NaN}, which JSON.parse rejects with a SyntaxError. In the other direction, json.loads("1e400") returns inf: the text is a valid JSON number, but it exceeds the largest binary64 value, about 1.8×103081.8 \times 10^{308}. The policy is to pass allow_nan=False on output and, on input, to supply a parse_constant that raises an error and a parse_float that converts the digits and rejects any result that is not finite.

Stated precisely

RFC 8259 fixes the syntax exactly. A number, for example, is: an optional minus sign; then either the single digit 0, or a nonzero digit followed by any number of digits; then, optionally, a decimal point followed by at least one digit; then, optionally, the letter e or E, an optional sign and at least one digit. The RFC states that numeric values that the grammar cannot express, such as infinity and NaN, are not permitted; that names within an object SHOULD be unique; and that a parser MUST accept all texts that conform to the grammar and MAY accept others.

Two conforming parsers are guaranteed to build the same value from a text only when all of the following hold.

  1. No object in the text has two members with the same name.

  2. Every number is an integer within ±(253−1)\pm(2^{53} - 1), or a value for which binary64 precision is enough for both programs.

  3. The text is valid UTF-8 and contains no escaped half of a surrogate pair without its other half.

  4. The text stays within the size and depth limits of both parsers.

  5. Neither parser applies an extension.

I-JSON (Internet JSON), the restricted profile that RFC 7493 defines [388], makes the first and third conditions requirements and recommends the second. An interface’s JSON policy is complete when it settles each of these five points and every parser on the path enforces it.

Limits and common mistakes

JSON has no single byte form for a value: {"a":1,"b":2} and { "b": 2, "a": 1 } hold the same object, and "A" and "\u0041" the same string. A signature or a hash over a JSON text covers its bytes, not its meaning. Verify the exact bytes received, then parse those same bytes; serializing the parsed value again does not reproduce them. Canonical JSON §12.28 schemes, such as the JSON Canonicalization Scheme of RFC 8785 [30], define one byte form for each value.

A check after parsing cannot see what parsing discarded. A dictionary built from a text with a duplicate JSON key no longer shows the duplicate, and a binary64 value no longer shows the digits that were rounded away; JSON Schema §12.27, which validates the parser’s output, inherits both losses. Enforce the policy inside the parser, through hooks or a strict parser, before any decision uses the value.

JSON has no types for dates, binary data or exact decimal fractions, and no comments. Applications carry such values in strings by convention, such as a date written as 2026-11-18 or bytes written in Base64, a text encoding of binary data. Each convention is part of the interface’s policy.

Lenient parsers, which accept comments, trailing commas or single quotation marks, follow the robustness principle §1.48. Between programs that must agree, strict parsing is the safer rule, because a text that one parser accepts and another rejects is itself a disagreement.

Parsing has costs. Python’s json.loads raised RecursionError on a text of 100,000 nested brackets, and a large text needs memory for the whole parsed value. Bound the size of the text before parsing.

Questions answered

What policy handles duplicate object names, non-finite numbers, and large integers?

The application’s own parsing policy handles them. JSON (JavaScript Object Notation), as RFC 8259 defines it, leaves all three open, so each interface must state a policy and every parser on the path must enforce it while parsing. A sound policy has three rules.

Apply the policy while parsing, because the parsed value no longer shows duplicates or the original digits.

Related entries: JSON Schema (§12.27), Canonical JSON (§12.28), Duplicate JSON key (§15.20), Authentication coverage (§5.41), Binary floating-point representation, special values, and rounding (§4.49), Robustness principle and strict parsing (§1.48), Unicode code points, scalar values, code units, and byte encodings (§12.48). Further reading: [29, 30, 124, 187, 271, 388].