What gets escaped

  • Double quotes become \".
  • Backslashes become \\.
  • Newlines become \n, tabs \t, carriage returns \r.
  • Control characters below U+0020 use \u00XX escapes.
  • Non-ASCII characters are preserved as UTF-8; JSON.stringify does not escape them by default.

Escaping vs serializing

Escaping produces the content of a JSON string, not a complete JSON document. To embed it in a document, wrap the result in double quotes. If you paste an escaped fragment into JSON.parse, it will fail because a bare string is not a document — wrap it first.

text
input:  He said "hello"
escaped: He said \"hello\"
as a JSON value: "He said \"hello\""

Unescaping safely

Unescaping accepts a JSON string literal, including the surrounding quotes, and returns the original text. Invalid escapes are reported instead of guessed, because a wrong guess can silently change the data.

Do not use escaping to sanitize untrusted input for HTML or SQL. It protects JSON syntax only; each output context needs its own encoding.