Skip to content
DevTinyTools

Syntax

JSON escape sequences: what parsers accept

Escape sequences look like a corner of JSON nobody argues about until a payload crosses languages. A string that parses in Ruby can fail in Node; a lone surrogate survives one parser and stops another. These are the results of running the same seven inputs through three real implementations, not a reading of the specification.

9 min readTool: JSON Escape

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.

How Node, Python and Ruby handle seven JSON escape sequences
SequenceIntentNodePythonRuby
\x41Write the letter A using the JavaScript short formBad escaped character in JSON at position 2 (line 1 column 3)Invalid \escape: line 1 column 2 (char 1)Accepted, produces x41
\0Write a null byte with the C-style NUL escapeBad 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 SQLBad escaped character in JSON at position 2 (line 1 column 3)Invalid \escape: line 1 column 2 (char 1)Accepted, produces '
\u12Write a Unicode character but one digit shortBad 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 formBad 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}"'
\ud800Write half of a surrogate pairAccepted, keeps the lone surrogateAccepted, keeps the lone surrogateincomplete surrogate pair at '\ud800"'
\/Escape a forward slash the way PHP's json_encode doesAccepted, 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 Ruby

If 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.

Serializer output for four characters
CharacterNodePythonRuby
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.

Frequently asked questions

Is \x41 valid JSON?
No. JSON defines \u followed by four hex digits, plus a short list of one-character escapes. Node and Python both reject \x41 with a bad-escape message. Ruby's JSON parser accepts it and produces the literal letter x41, so a payload that works in one service can fail in another.
Why does a lone surrogate survive in Node but fail in Ruby?
Node keeps the code unit exactly as written, because JSON.stringify and JSON.parse operate on JavaScript strings. Ruby's parser checks that surrogate pairs are complete and rejects the half pair. The bytes on the wire are the same; the parsers apply different post-conditions.
Does Python always escape non-ASCII characters?
json.dumps does by default, with ensure_ascii=True. The strings café, an emoji and 用户 become \u00e9, a surrogate pair and three \u escapes. Pass ensure_ascii=False and the output keeps the characters, which is smaller in UTF-8 but no longer ASCII-only.
Which escape should I use for a forward slash?
Either. \/ is legal JSON and produces the same character as a bare slash. Some serializers emit it to avoid closing a script tag in embedded HTML. It never changes the parsed value, only the bytes in the document.