STRUCTURED TEXT

Comparing escaped and literal Unicode text in formatted JSON

A JSON string can spell an accented letter directly or through a backslash-u escape. The formatter keeps whichever spelling the source used, so two files that mean the same text can still differ character by character.

Two spellings of the same letter

JSON allows a string to contain almost any Unicode character as itself. It also allows a six-character escape: a backslash, the letter u and four hexadecimal digits. The letter é has code point U+00E9, so the document {"name":"é"} and the document {"name":"\u00e9"} describe one and the same value for any conforming parser. The text of the two files is different, though. One contains a single accented character in the name field, the other contains a backslash followed by five more characters. The two cases below put both spellings through the formatter with an indent of 0, which compacts the document, and each result keeps the spelling it started with.

What the formatter changes and what it leaves alone

Formatting a JSON document means parsing it and printing it again with the layout you ask for. The tool changes whitespace between tokens: spaces after colons, line breaks, indentation. It does not decode the string token and print it again in a preferred style. That choice is deliberate, because rewriting every escape into a literal character, or the reverse, would alter the source text without being asked. If you paste {"name":"\u00e9"} with an indent of 0, the output is the same 17-character line you pasted. Pasting {"name":"é"} gives back the literal-character line. Neither run reports a problem, since both inputs are valid JSON, so the result shows valid as true in each case.

The rules JSON sets for strings

The JSON grammar in RFC 8259 and ECMA-404 requires only a few characters to be escaped inside a string: the double quote, the backslash and the control characters from U+0000 to U+001F. Everything else may appear literally or as a four-digit escape, at the writer's choice. Characters outside the Basic Multilingual Plane, such as an emoji at U+1F600, can only be escaped as a pair of surrogate escapes, for example \ud83d\ude00. Letters in the escape digits may be upper or lower case, so \u00E9 and \u00e9 are two more spellings of the same letter. A tool that preserves source spelling therefore has several legitimate forms to keep apart, which is why a formatted file can differ from a colleague's version of the same data.

Why other tools disagree about the default

Different libraries write strings differently, and that explains many diffs between files that were exported by different systems. Python's json.dumps escapes every non-ASCII character by default, so é comes out as \u00e9 unless the caller sets ensure_ascii to false. JavaScript's JSON.stringify writes the literal é and escapes only the characters the grammar requires, plus lone surrogates. A configuration file saved by one program and re-saved by another can flip between spellings without any change in meaning. When a version control diff lights up on a line that looks identical on screen, an escape on one side and a literal character on the other is a likely cause, and the two cases here are the smallest reproduction of it.

Comparing decoded values

If you need to know whether two documents contain the same data, compare parsed values, not source text. Parse both files with the library that your consumer uses, then compare the resulting string objects. Under that comparison the escaped and literal spellings of é are equal, and so are upper-case and lower-case hexadecimal digits. This formatter does not perform that semantic comparison and it does not normalize Unicode. A precomposed é and an e followed by a combining accent are still different strings after parsing, which the Unicode pages in this series examine separately. The formatter helps with the first step only: confirming that the JSON is valid and seeing exactly which spelling each file holds.

When the exact source spelling matters

Some workflows treat the file as bytes, not as data. A digital signature, a checksum or a content hash of the source file changes when an escape becomes a literal character, even though the parsed content is identical. Test fixtures, snapshot files and cached responses can also fail an exact match for the same reason. In those cases keep the original escapes and avoid passing the file through a tool that rewrites strings. RFC 8785, the JSON Canonicalization Scheme, defines one agreed spelling for signing purposes, and it writes characters literally except where escaping is required. This formatter is not a canonicalizer, so its output should not be fed into a signature step that expects that scheme.

Reading the two sample cases

The first case enters {"name":"\u00e9"} with an indent of 0 and returns the same text, valid true. The note beside it says the six-character escape stays in the source, which you can confirm by counting characters in the output. The second case enters {"name":"é"} and also returns it unchanged. Run them one after the other and set the outputs next to each other in any text comparison tool. The comparison reports a difference on the name line even though both lines mean the same thing. Then change the indent to 2 for either input and you will see the layout expand over three lines while the string token itself still reads exactly as before.

Characters beyond U+FFFF and the surrogate pair

The four-digit escape can only name code points up to U+FFFF. A character above that, such as the grinning face at U+1F600, is written in escaped form as two consecutive escapes that form a UTF-16 surrogate pair, here \ud83d\ude00. Written literally, the same character is one character in the file and four bytes in UTF-8. Parsers join the pair back into one character, so the two spellings again produce one value. A lone surrogate escape without its partner is allowed by the grammar but does not form a valid character, and consumers treat it differently, so it deserves a check before data is passed along. The formatter preserves pairs the way it preserves every other escape.

Practical habits for mixed sources

When you merge data from several systems, decide on a spelling policy before the merge and apply it with the tool that owns the data, such as the serializer in your own code. Record the policy beside the example you share so a teammate can reproduce a byte-identical file. Check validity first, because an escape with fewer than four hex digits, such as \u00e, is a syntax error in strict JSON, and the formatter will report it instead of guessing. Keep a copy of the original before any bulk reformatting. For human review of a diff, a quick parse-and-compare step in your language of choice removes the noise that spelling differences create.

Reproduce the checks

Escape spelling

Open the Json Validator Formatter and enter:

JSON input
{"name":"\u00e9"}
Indent
0

Expected output:

Valid

true

Formatted

{"name":"\u00e9"}

The six-character escape spelling stays in the JSON source.

Literal character spelling

Open the Json Validator Formatter and enter:

JSON input
{"name":"é"}
Indent
0

Expected output:

Valid

true

Formatted

{"name":"é"}

The literal character is also preserved.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks