JSON Escape Unescape
Published Updated
Turn raw text into a quoted JSON string, or turn an escaped string back into plain text, as you type. Quotes, backslashes and line breaks are escaped; accented letters and emoji are left as they are, and the outer quotes are optional when unescaping.
What gets escaped
RFC 8259 says only the quotation mark, the reverse solidus and the control characters U+0000 to U+001F must be escaped in a string. This page follows that and leaves everything else alone. Results from Google Chrome 156:
| Character in the text | Written as |
|---|---|
| Double quote | \" |
Backslash, as in C:\temp | \\, so "C:\\temp" |
| Line break, tab | \n, \t |
| Other control characters, such as U+0001 | \u0001 |
/ and </script> | Unchanged |
| é, 日本, an emoji | Unchanged |
The same function fixes a lone surrogate by writing it as a \u escape, as MDN describes for well-formed JSON.stringify.
Each control character and separator, as written
The page's own Escape code ran in Google Chrome 158, Firefox 155, Playwright's WebKit build (not the Safari app) and Node.js 22.23.1. The measured output was the same in all four. Every one of the 32 control characters U+0000 to U+001F was run on its own; the other rows are the characters named.
| Character in the text | Written as |
|---|---|
| U+0008, U+0009, U+000A, U+000C, U+000D | "\b", "\t", "\n", "\f", "\r" |
| The other 27 control characters, such as U+0000, U+0007, U+000B and U+001F | "\u0000", "\u0007", "\u000b", "\u001f": four lowercase hex digits |
| Space, DEL (U+007F), U+0080, U+009F, no-break space (U+00A0), U+FEFF | Unchanged |
| Line separator U+2028 and paragraph separator U+2029 | Unchanged |
| Apostrophe, <, & | Unchanged |
| A lone high surrogate U+D800, a lone low surrogate U+DC00 | "\ud800", "\udc00" |
| U+D800 followed by a, and a low surrogate followed by a high one | "\ud800a", "\udc00\ud800" |
| A surrogate pair that forms one emoji | Unchanged |
5 of the 32 control characters get a two-character escape, and the other 27 get a six-character \u escape. RFC 8259 allows both forms and says the hexadecimal letters can be uppercase or lowercase. The ECMAScript JSON.stringify definition escapes every code point below U+0020 and every surrogate that is not part of a pair, so a lone surrogate never appears in the output.
What Unescape accepts, refuses and returns
Each input was typed into the page in Unescape mode. The same 39 inputs gave the same result in Chrome 158, Firefox 155 and the WebKit build, and the same result from the library in Node.js: 22 were accepted and 17 were refused. A refused input shows this message under the result: Couldn't parse this as an escaped JSON string — check for a stray unescaped quote or backslash. In the table, <LF>, <CR>, <TAB> and <U+XXXX> stand for the real characters, while \n and \u0001 written with a backslash are the two-character sequences.
| Input to Unescape | Result |
|---|---|
Line one\nLine two, "Line one\nLine two" | Line one<LF>Line two, Line one<LF>Line two |
\u00e9, \u00E9 | é, é |
\ud83d\ude00 | 😀 |
\ud83d, \ude00 | <U+D83D>, <U+DE00> |
\/, \\ | /, \ |
\b\f\r\t | <U+0008><U+000C><CR><TAB> |
"" | an empty string |
123, true | 123, true |
"a"<LF>, "a\tb"<CR><LF>, "a" , <TAB>"a"<TAB> | a, a<TAB>b, a, a |
| Three spaces; the text a b with one space on each side | Returned unchanged, spaces included |
a<U+007F>b, a<U+2028>b, café 😀 | a<U+007F>b, a<U+2028>b, café 😀 |
\', a<LF>b, a<TAB>b, a<U+0001>b, a"b, "a" and "b", ", \x41, \a, \U0041, \u12, \u00zz, \, "a\", \u{1F600}, "a<LF>b", <U+00A0>"a" | Refused, with the same message each time |
Spaces, tabs and line breaks around a quoted string are ignored, as the JSON grammar allows around a value. A no-break space is not JSON whitespace, so it makes the input fail. The escape \u12 is refused because a four-digit sequence is required, while \ud83d on its own is accepted and gives a lone surrogate.
Round trips and speed
2,000 generated strings of 1 to 12 characters, drawn from 26 characters that include quotes, backslashes, control characters, U+2028, U+FEFF, emoji and lone surrogates, were escaped and then unescaped, once with the quotes and once with the quotes removed. All 2,000 came back unchanged in both ways, and 753 of the strings held a lone surrogate. Every escaped output was free of lone surrogates (2,000 of 2,000). Time in milliseconds, median of 3 runs, for a generated text of 1,000,032 characters with quotes, line breaks, backslashes, accents and emoji that escapes to 1,303,074 characters:
| Runtime | Escape | Unescape |
|---|---|---|
| Node.js | 7 | 6 |
| Chrome 158 | 6 | 4 |
| Firefox 155 | 3 | 3 |
| WebKit build | 2 | 6 |
A path that shows the problem
Paste C:\temp\new into a JSON value by hand and a parser reads a tab and a newline where the folder names start. Escaped, it is "C:\\temp\\new", and Unescape returns the original path.
The reverse mistake is as common: text copied from a JSON file with its backslashes still in it. Switch to Unescape and the real line breaks come back.
Using the two modes
Step 1
Pick Escape or Unescape
Escape takes raw text such as a log line or a block of SQL. Unescape takes a string that already has backslash sequences in it.
Step 2
Paste the text
The result updates on every keystroke, so there is no convert button. Escape output always includes the surrounding double quotes.
Step 3
Copy Result
Paste the escaped string into a JSON file, a request body or a test fixture. For unescape, the plain text appears in the second box.
When unescape refuses
- A single quote written as \' in the input.
- A raw line break or tab in the middle of the string.
- A quote in the middle that is not preceded by a backslash.
- A backslash followed by a letter JSON does not define, such as \x or \a.
The error text asks you to look for a stray quote or backslash. For whole documents, JSON Formatter shows the position of the first problem.
Escaping questions
Do I include the quotes when I unescape?
Either way works. Input that starts and ends with a double quote is parsed as it stands (spaces and line breaks around it are ignored), and anything else is wrapped in quotes first. A text such as "a" and "b" starts and ends with quotes but has two strings in it, so it fails.
Why does \' fail to unescape?
JSON has no escape for a single quote, so the sequence is invalid. A single quote needs no escape in a double-quoted string and should be written bare.
Why does a real line break fail in unescape mode?
A raw newline or tab inside a JSON string is not allowed. They must appear as \n and \t. Text copied with real line breaks belongs in Escape mode.
Why are accents and emoji not escaped?
JSON only requires escaping for the quotation mark, the backslash and control characters. The page leaves café and emoji as they are, and unescape reads \u00e9 and escaped surrogate pairs, such as the two escapes for U+D83D and U+DE00 that make 😀, back correctly.
Does it escape the forward slash or a closing script tag?
No. The slash is legal bare, so a closing script tag in your text stays intact. If the JSON will sit inside an HTML script element, escape that sequence yourself.
Is my text sent anywhere?
No. The page calls JSON.stringify and JSON.parse on the text, and there is no network request. API keys and logs stay in the tab.
Which control characters become \n and which become \u0001?
Five get a short escape: backspace as \b, tab as \t, line feed as \n, form feed as \f and carriage return as \r. The other 27 of the 32 characters from U+0000 to U+001F are written as \u followed by four lowercase hex digits, for example \u0000 and \u001f.
Does it change U+2028, DEL or a lone surrogate?
U+2028, U+2029 and DEL (U+007F) are left as they are, because JSON allows them inside a string. A lone surrogate such as U+D800 is written as the six characters \u followed by d800, so the result is well-formed in Chrome 158, Firefox 155 and the WebKit build.
Can I paste a quoted string that ends with a line break?
Yes. Spaces, tabs and line breaks around a quoted string are ignored, so a quoted string followed by a line break unescapes to its text. A line break between the quotes is still refused, and so is a no-break space outside them.
Does escaping and then unescaping give back the original text?
In the test it did. Of 2,000 generated strings, all 2,000 came back unchanged, with or without the surrounding quotes, including 753 strings that held a lone surrogate.