Seven sequences, three parsers
Each cell below is the literal output of feeding the sequence to JSON.parse in Node 22.22.2 (V8), json.loads in Python 3.13.12 (json), or JSON.parse in Ruby 2.6.10 (json 2.1.0). Nothing here is inferred from documentation.
| Sequence | Intent | Node | Python | Ruby |
|---|---|---|---|---|
| \x41 | Write the letter A using the JavaScript short form | Bad escaped character in JSON at position 2 (line 1 column 3) | Invalid \escape: line 1 column 2 (char 1) | Accepted, produces x41 |
| \0 | Write a null byte with the C-style NUL escape | Bad escaped character in JSON at position 2 (line 1 column 3) | Invalid \escape: line 1 column 2 (char 1) | Accepted, produces 0 |
| \' | Write a single quote as if this were JavaScript or SQL | Bad escaped character in JSON at position 2 (line 1 column 3) | Invalid \escape: line 1 column 2 (char 1) | Accepted, produces ' |
| \u12 | Write a Unicode character but one digit short | Bad Unicode escape in JSON at position 5 (line 1 column 6) | Invalid \uXXXX escape: line 1 column 3 (char 2) | unexpected token at '"\u12"' |
| \u{1F600} | Write an emoji using the ES2015 braced form | Bad Unicode escape in JSON at position 3 (line 1 column 4) | Invalid \uXXXX escape: line 1 column 3 (char 2) | unexpected token at '"\u{1F600}"' |
| \ud800 | Write half of a surrogate pair | Accepted, keeps the lone surrogate | Accepted, keeps the lone surrogate | incomplete surrogate pair at '\ud800"' |
| \/ | Escape a forward slash the way PHP's json_encode does | Accepted, produces / | Accepted, produces / | Accepted, produces / |
Three patterns stand out. A JavaScript-style short escape is rejected by two parsers and accepted by the third. A braced emoji is rejected everywhere, because JSON has no braced form. A forward slash is accepted everywhere, escaped or not.
The surrogate split
A Unicode code point above U+FFFF is written in JSON as two surrogate escapes: a high half and a low half. If only the high half arrives, Node and Python keep it as a lone code unit while Ruby refuses the string. That difference is not a typo in one implementation; it is a decision about whether a parsed string must be well-formed Unicode.
JSON.parse('"\\ud800"') // accepted in Node
json.loads('"\\ud800"') # accepted in CPython
JSON.parse('"\\ud800"') # incomplete surrogate pair in RubyIf a payload crosses that boundary, the symptom looks like a transport bug: one service parses the body and another rejects the identical bytes. The escape tool shows which sequences are legal before the document leaves your machine.
What the serializer writes
Parsing is only half the story. A serializer decides whether to keep a character as UTF-8 or write it as an escape. The four cells below are the actual output of handing one character to each serializer with default arguments.
| Character | Node | Python | Ruby |
|---|---|---|---|
| Accented letter é | é | \u00e9 | é |
| Emoji 😀 | 😀 | \ud83d\ude00 | 😀 |
| Forward slash / | / | / | / |
| Vertical tab | \u000b | \u000b | \u000b |
On the sample string café ☕ 用户, Python's default output is 31 characters and 31 bytes. With ensure_ascii=False it is 11 characters and 18 bytes. The escaped form is portable through ASCII-only channels; the raw form is almost three times smaller.
Practical rules
- Write \\uXXXX, not \\xXX. It is the only general escape for characters outside the short list.
- Keep surrogate pairs together. A single high half is not a character, even if one parser tolerates it.
- A bare slash is fine. Escape it only when embedding JSON inside a script tag where
</script>would end the element. - Pick one serializer policy. ASCII-only or raw UTF-8 is fine; mixing them makes byte-size comparisons meaningless.