Full name: Canonical JavaScript Object Notation (JSON).
Definition
Canonical JSON assigns one byte representation to each JSON §12.26 (JavaScript Object Notation) value a particular scheme accepts. Ordinary JSON allows many spellings of the same data: spaces may be added, object members may come in any order, and one number may be written 1, 1.0 or 1e0. A canonical form fixes every such choice, so two programs holding the same data produce identical bytes, which can be hashed, signed or compared. Several schemes exist; RFC 8785 publishes the JSON Canonicalization Scheme (JCS).
The idea
A signature, a message authentication code §5.7 and a hash are all computed over bytes, not over meaning. Suppose a server signs the 13 bytes {"b":2,"a":1}. A client parses them into a dictionary and later writes the dictionary out again to check the signature. Its JSON library prints {"a": 1, "b": 2}: the same data in different bytes, and the check fails. Worse, a sender and a receiver that format one record differently can each believe that a different byte string was authenticated.
There are two ways out. One is to keep the exact bytes that were signed, verify those, and never write the data out again. The other is a canonical form: a rule that both sides apply to the parsed data, which yields the same bytes from the same data however the data was formatted on the way. A canonical form also lets a program hash a value to give it a stable identifier, or compare two configurations byte by byte. An everyday picture is writing every date as year, month and day with a fixed number of digits, 2026-11-18, so that each date has one spelling. The picture stops early: a date needs only a fixed spelling, while JSON also needs a fixed order for the members of an object and a fixed way to write each number and each character.
How it works
JCS works on a restricted data model called I-JSON (Internet JSON), defined in RFC 7493 [388]. I-JSON forbids duplicate object member names and restricts strings to interoperable Unicode §12.48 text. It advises senders to stay within the magnitude and precision of double-precision numbers. JCS requires its number data to be expressible in the IEEE 754 §4.49 double-precision format [271], called binary64. JCS is defined only for data in this model, so input with a repeated member name, or with a number too large for double precision (beyond about ), must be refused before canonicalization. So must the values NaN (not a number) and infinity, which a program’s data can contain although JSON text cannot express them. Characters are named here by their Unicode code point, the number Unicode assigns to each character, written U+ followed by hexadecimal digits: U+0020 is the space and U+00E9 is the letter é.
Canonicalization then proceeds as follows.
Parse the JSON text into values: objects, arrays §14.5, strings, numbers,
true,falseandnull. Spaces and line breaks between the parts disappear here.Literals:
true,falseandnullare written as these words.Numbers: each number is written by the rule that the JavaScript language uses to turn a number into text, specified in ECMAScript, the standard that defines JavaScript. The rule picks the shortest decimal digits that convert back to exactly the same double-precision value. A whole number whose size is below is written without a decimal point or an exponent:
1.0becomes1, and1e2becomes100. Trailing zeros vanish:2.50becomes2.5. Very large and very small sizes use an exponent: is written1e+21and is written1e-7, while is written0.000001. Negative zero is written0.Strings: each string is written between double quotes with as few escapes as possible. Only the double quote, the backslash and the control characters below
U+0020are escaped. The double quote becomes\"and the backslash\\. Five control characters use short forms:\b,\t,\n,\fand\r. The other control characters use\ufollowed by four lowercase hexadecimal digits. Every other character, including every character outside ASCII, is written as itself, never as a\uescape.Objects: the members are sorted by name, and names are compared as sequences of UTF-16 (Unicode Transformation Format, 16-bit) code units. UTF-16 writes each character as one or two 16-bit numbers, called code units, and the comparison treats each code unit as an unsigned number. The comparison ignores language and locale settings. Members are written as name, colon, value, separated by commas, with no spaces, and the sort applies inside every nested object.
Arrays keep their order, because the order of array items is part of the data.
Encoding: the resulting text is encoded in UTF-8 (Unicode Transformation Format, 8-bit), which writes each character as one to four bytes.
Sorting by UTF-16 code units can differ from sorting by code points. A character above U+FFFF, such as the emoji U+1F600, takes two UTF-16 code units, the first of which is 0xD83D. The Hebrew letter U+FB33 takes one code unit, 0xFB33. JCS compares 0xD83D with 0xFB33 and puts the emoji first, although its code point is the larger number. A program that sorts strings by code point, as Python §12.23 does when it compares strings, puts the emoji last.
Worked example
Two senders hold the same record and transmit it in two spellings:
Text A:
{"name": "caf\u00e9", "b": 2.50, "a": [1.0, 1e2]}
Text B:{"a":[1,1E+2],"name":"café","b":25e-1}
Text A writes é as the escape \u00e9; text B writes the character itself.
Numbers.
1.0and1are the double-precision value 1, written1.1e2and1E+2are both , written100.2.50and25e-1are both , a value that double precision holds exactly, written2.5.Strings. The escape
\u00e9in text A and the raw character in text B both denote the code pointU+00E9, so both strings are the four characters c, a, f, é. JCS writes é as itself.Sorting. The names are
"a", whose code unit is 97,"b"(98) and"name", whose first code unit is 110, the letter n. The order isa,b,name.Result. Both texts give
{"a":[1,100],"b":2.5,"name":"café"}. This text has 35 characters. In UTF-8 the letter é takes the 2 bytes0xC3 0xA9and each of the other 34 characters takes 1 byte, so the canonical form is bytes:7b2261223a5b312c3130305d2c2262223a
322e352c226e616d65223a22636166c3a9227d
Both senders obtain the same digest when they hash these 36 bytes with the same hash function. A signature from either sender can be verified over those bytes using that sender’s verification key. Even a single signer can produce different valid signatures if the scheme uses fresh randomness.
Sorting the keys and removing spaces with a general JSON library does not give these bytes. Python’s json.dumps(d, sort_keys=True, separators=(",", ":")), applied to the parsed record d, prints {"a":[1.0,100.0],"b":2.5,"name":"caf\u00e9"}, 44 bytes: it writes floating-point numbers as 1.0 and 100.0, and by default it escapes é. For example, the SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) digest of the 44-byte text begins with 4c89504d, while the digest of the 36 canonical bytes begins with a473afad.
Stated precisely
Let be the set of values accepted by JCS, and let be the JCS byte string of a value in . Two values are equal when they have the same type and
as numbers, they are the same double-precision value, with negative zero counted as zero, so
1,1.0and1e0are equal;as strings, they are the same sequence of code points;
as arrays, they have equal items in the same order;
as objects, they have the same set of names and equal values for each name, in any order.
The contract of JCS has three parts.
depends only on .
If equals , then .
is valid JSON text that parses back to a value equal to . Hence two values that are not equal never share canonical bytes.
Part 3 is what makes it safe to sign : the signature then binds the value and nothing else. The contract assumes that both sides parse with the I-JSON data model and refuse whatever lies outside it, and that both format numbers exactly by the ECMAScript rule. Number formatting needs the most care, so test an implementation against the number examples in RFC 8785.
JCS does not apply Unicode normalization §15.19, the conversion of different code point sequences that represent the same text to one chosen form. The letter é can be the single code point U+00E9, 2 bytes in UTF-8 (0xC3 0xA9), or the letter e (U+0065) followed by a combining acute accent (U+0301), 3 bytes in UTF-8 (0x65 0xCC 0x81). The two look identical on screen, yet they are different strings for JCS and give different canonical bytes. Those bytes are distinct inputs to a hash function. An application that wants them treated as equal must normalize them itself, for example to NFC (Normalization Form C, the composed form), before signing, and the verifier must apply the same rule.
Limits and common mistakes
Canonical bytes are not authentication §1.6. Equal bytes show only equal data. A digital signature §5.14 or a message authentication code over the bytes is what ties them to a key holder, and it must cover the right fields; the authentication coverage §5.41 entry treats that question.
Lossy parsing before canonicalization destroys distinctions that canonicalization cannot recover. The integer has no exact double-precision value and becomes , so two different 64-bit identifiers can share canonical bytes. I-JSON recommends sending numbers that need more range or precision than double precision offers as strings.
A parser that silently keeps the last of two members with the same name has already chosen a value; canonicalizing afterwards hides that the sender sent two. Reject repeated names while parsing, as the duplicate JSON key entry explains.
“Canonical JSON” is not one scheme. Other projects define their own canonical forms, which differ from JCS in details such as whether fractional numbers are allowed and which characters are escaped. Name the scheme exactly: bytes from one scheme do not verify under another.
Canonicalization does not check meaning. It does not validate version numbers, permissions or freshness, and a canonical record §13.13 can be an old record sent again in a replay attack §1.26.
Act on what was verified. After verifying a signature over , use the value that was canonicalized, not a second parse of the original text by a different parser, which may disagree about repeated names or large numbers.
Questions answered
How are number formatting, property sorting, and Unicode handled?
In the JSON Canonicalization Scheme (JCS, RFC 8785), the canonical form of JSON (JavaScript Object Notation) data, numbers are written in the shortest form that JavaScript uses, object members are sorted by their names compared as UTF-16 code units, and strings keep their exact Unicode characters, written with minimal escapes and encoded in UTF-8. In detail:
Numbers. Every number is an IEEE 754 double-precision floating-point value. JCS writes the shortest decimal that converts back to that value, by the rule of ECMAScript, the JavaScript standard:
1.0becomes1,2.50becomes2.5,1e2becomes100, becomes1e+21, becomes1e-7, and negative zero becomes0. NaN (not a number) and infinity are refused. Integers beyond can lose precision, so they should travel as strings.Property sorting. The members of every object, at every depth, are sorted by name. Names are compared as sequences of UTF-16 (Unicode Transformation Format, 16-bit) code units, each treated as an unsigned 16-bit number, independent of locale:
"a"(97) precedes"b"(98). Array order is kept.Unicode. Strings are not normalized: é as one code point (
U+00E9) and as e followed by a combining accent (U+0065,U+0301) stay different. Only the double quote, the backslash and the control characters belowU+0020are escaped. Every other character is written as itself, and the output is encoded in UTF-8 (Unicode Transformation Format, 8-bit).
Other canonical JSON schemes make different choices, so a protocol must name its scheme. Sorting keys and removing whitespace with a general JSON library does not produce JCS bytes.
Related entries: JSON (§12.26), Authentication coverage (§5.41), Unicode normalization (§15.19), Integrity (§1.9), Duplicate JSON key (§15.20), Unicode code points, scalar values, code units, and byte encodings (§12.48), Serialization (§4.28), Digital signature (§5.14). Further reading: [30, 271, 388].