String Escape / Unescape
Escape newlines, quotes, backslashes and control characters so text can be embedded in a JSON or JavaScript string, or unescape \n, \" and \u00e9 sequences back to plain text.
What does escaping a string mean?
Escaping replaces the characters that cannot appear literally inside a quoted string with backslash sequences. In JSON those characters are the double quote, the backslash and the control characters U+0000 to U+001F, which include newline and tab; the rules are in RFC 8259, section 7. This tool escapes with your browser’s own JSON.stringify and unescapes with JSON.parse, so its output is exactly what JavaScript would produce and accept.
Because a JSON string literal is also a valid JavaScript string literal, the escaped output can be pasted into a .json file, a request body or .js/.ts source code without changes.
How to use the string escaper
- Type or paste text into the Input pane, or load it with File (read locally) or URL (fetched by your browser; the other site must allow cross-origin requests).
- Click Escape to turn the whole input, line breaks included, into one double-quoted literal.
- Click Unescape to turn an escaped string back into plain text. Surrounding quotes are optional.
- Use Copy or the copy icon in the Output header, or Download to save the result as
string-result.txt. Clear empties both panes.
What Escape and Unescape do
- Escape always wraps the result in double quotes. It writes
\"and\\for quotes and backslashes, the short forms\n,\r,\t,\band\fwhere they exist, and\u00XXfor the other control characters. Accented letters, other scripts and emoji are left as they are, because JSON allows any Unicode character inside a string. Single quotes and forward slashes are not escaped. - Unescape accepts exactly the escapes JSON defines:
\",\\,\/,\b,\f,\n,\r,\tand\uXXXX. A surrogate pair such as\ud83d\ude00becomes a single emoji. If the input is not already wrapped in double quotes, the tool adds them and escapes any bare"for you, so a fragment copied out of a log line works too. Line breaks before or after the text are ignored; spaces are kept, because they are part of the string.
Line breaks always come out as \n, never \r\n: browsers convert the line breaks in a text area to a single line-feed character, even when the pasted or loaded text used Windows line endings.
When unescaping fails, the message after Could not unescape: comes from your browser’s JSON parser, so the wording differs between Chrome, Firefox and Safari.
Examples
Two lines of text containing quotes and a Windows path:
He said "hello"
Path: C:\Users\ada
escape to a single-line literal:
"He said \"hello\"\nPath: C:\\Users\\ada"
| Input | Escape output | Note |
|---|---|---|
Tab here (with a real tab) | "Tab\there" | Control characters with a short form use it |
| A bell character, U+0007 | "\u0007" | Other control characters use \u |
| A line break | "\n" | Also for pasted Windows line endings (see below) |
It's fine | "It's fine" | Single quotes need no escape |
café 😀 | "café 😀" | Non-ASCII text is kept as UTF-8 |
</script> | "</script>" | Unchanged; see the HTML warning below |
Unescaping caf\u00e9 returns café, and C:\\Users\\ada returns C:\Users\ada.
JSON escapes versus JavaScript escapes
JavaScript string literals, defined in the ECMAScript specification, accept everything JSON does plus several extra forms. Escape never produces the extra forms, but Unescape rejects them, because they are not valid JSON.
| Sequence | Meaning | JSON | JavaScript |
|---|---|---|---|
\n, \t, \", \\ | Newline, tab, quote, backslash | Yes | Yes |
\u00e9 | Four-digit code unit | Yes | Yes |
\' | Single quote | No | Yes |
\x41 | Two-digit hex escape | No | Yes |
\u{1F600} | Code point escape | No | Yes (ES2015+) |
\v, \0 | Vertical tab, null | No | Yes |
Common problems and fixes
| Problem | Cause | Fix |
|---|---|---|
| Unescape reports a bad escaped character | The input uses a JavaScript-only escape such as \' or \x41. | Replace \' with ' and \xHH with \u00HH. |
| Unescape reports a bad control character | The input contains a real line break or tab in the middle; JSON strings must not. | Escape the text first, or remove the raw line breaks. |
| Unescape reports an unterminated string | A single backslash at the very end escapes the closing quote. | Remove it, or double it to \\. |
| An inline script stops early in the browser | A literal containing </script> was placed inside an HTML <script> element, which the HTML parser closes at that point. | Escape every < in the literal as \u003c, which is valid in both JSON and JavaScript and also covers <!--; see Escaping HTML correctly. |
| The output has quotes you did not want | Escape always returns a complete literal. | Delete the first and last character if you are inserting into an existing pair of quotes. |
Frequently asked questions
How is escaping different from URL encoding or HTML entities?
Each one protects text for a different parser. String escaping uses backslashes for JSON and JavaScript literals, URL encoding uses %XX bytes for URLs, and HTML entities such as < protect markup. Use the one that matches where the text ends up; applying the wrong one leaves the real problem characters unprotected.
Can I put a whole JSON document inside a string?
Yes. Paste the JSON and click Escape to get a string that contains it, which is what some message queues, logging pipelines and configuration fields expect. To reverse it, Unescape the string and then format the recovered JSON with the JSON Formatter.
Why are é and emoji not turned into \u sequences?
JSON text is UTF-8 and may contain any Unicode character directly, so JSON.stringify only escapes the characters that are not allowed. If a system really needs ASCII-only output, this tool does not offer that option; most JSON libraries have one, for example ensure_ascii in Python’s json module, which is on by default.
Will the output work in languages other than JavaScript?
Any JSON parser accepts it, and the core sequences used here are shared by many C-style languages such as Java, C# and Go. Check the target language before pasting into source code: PHP, for example, writes Unicode escapes as \u{e9} and expands $variables inside double-quoted strings.
Why does my log message show \n instead of line breaks?
Structured loggers write each entry as JSON, so line breaks and quotes inside a message are stored escaped. Paste the message into the Input pane and click Unescape to read it as it was written; a stack trace becomes readable again.
Is my text uploaded anywhere?
No. Escaping and unescaping run in your browser, and File reads the file locally. The only network request the tool makes is when you use URL, and that request goes from your browser straight to the address you enter.
Guides
Common JSON Syntax Errors and How to Fix Them
Trailing commas, single quotes, comments, unescaped characters and other reasons JSON fails to parse, with the error each one produces and the fix.
Escaping HTML Correctly: Entities, Contexts and XSS
When and how to escape text for HTML, attributes, JavaScript and URLs, which entities matter, and why context-aware escaping prevents XSS.