How to escape or unescape a JSON string
- Pick a mode. Escape turns plain text into the inside of a JSON string. Unescape does the reverse.
- Paste your text. In Unescape mode you can paste the string with or without its surrounding double quotes.
- Set options if needed. Wrap the result in quotes, convert non-ASCII characters to
\uXXXX, make the output safe for an HTML<script>block, or escape forward slashes. - Copy the result. If unescaping fails, the error names the position and the reason.
What JSON requires
A JSON string is wrapped in double quotes. RFC 8259, section 7, says three groups of characters must be escaped inside it:
- the double quote
", written\" - the backslash
\, written\\ - control characters U+0000 to U+001F, such as line feed and tab
Two-character escapes exist for the common cases: \" \\ \/ \b \f \n \r \t. Any other character can be written as \u plus four hex digits. Characters above U+FFFF use a surrogate pair, so the G clef U+1D11E is \uD834\uDD1E.
Nothing else needs escaping. Accented letters, Cyrillic, Chinese and emoji are valid as they are in UTF-8 JSON.
Examples
| Plain text | Escaped for JSON |
|---|---|
He said "hi" |
He said \"hi\" |
C:\path\file |
C:\\path\\file |
line1 (line break) line2 (tab) tab |
line1\nline2\ttab |
{"a":1,"b":"x"} |
{\"a\":1,\"b\":\"x\"} |
</script>& with HTML-safe on |
\u003c/script\u003e\u0026 |
é😀 with non-ASCII escaping on |
\u00e9\ud83d\ude00 |
Errors you may see when unescaping
| Input | Problem |
|---|---|
\x41 |
\x is not a JSON escape (it is valid in JavaScript and Python, not JSON) |
\u12 |
A \u escape needs exactly four hex digits |
| A raw line break | Strict JSON does not allow it inside a string; use Lenient or write \n |
He said "hi" |
The inner quotes are not escaped, so the string ends early |
Escaping in code
Every major language has a built-in that does the same job, which is safer than hand-written replacements:
console.log(JSON.stringify('He said "hi"')); // "He said \"hi\""
import json
print(json.dumps('He said "hi"')) # "He said \"hi\""
print(json.dumps("é")) # "\u00e9" (escapes non-ASCII by default)
print(json.dumps("é", ensure_ascii=False)) # "é"
Typical uses
- Embedding JSON inside JSON, for example a request body stored as a string field in a log entry or webhook payload.
- Reading log lines that show
\"and\neverywhere. Unescape to see the original text with real line breaks. - Putting JSON into an HTML page. Turn on the HTML-safe option so
</script>cannot close the script element early. - Preparing strings for config files that must stay ASCII.
Related tools
JSON escaping is often confused with other formats. \u00e9 here is the same notation as a JavaScript Unicode escape; the Unicode escape converter handles \u{...}, Python and HTML forms too. If the JSON travels inside a URL, it also needs URL encoding, which the universal encoder decoder handles. For tokens whose payload is Base64url-encoded JSON, use the JWT decoder.
The tool uses your browser's own JSON.stringify and JSON.parse; nothing is sent to our server.
Frequently asked questions
Which characters must be escaped in JSON?
RFC 8259 requires escaping the double quote, the backslash and the control characters U+0000 to U+001F. Everything else, including non-ASCII letters and emoji, may appear as is in UTF-8 JSON, although any character may also be written as a \uXXXX escape.
Do I need to escape the forward slash?
No. JSON allows \/ as an escape for /, but it is optional. Some code escapes it so that the sequence cannot appear inside JSON embedded in an HTML page. The tool offers this as an option.
Why does unescaping fail with 'Bad control character'?
The input contains a raw line break or tab inside the string. Strict JSON does not allow that; it must be written as \n or \t. Turn on 'Lenient' to accept raw line breaks and tabs.