String Escape Sequences in JSON and JavaScript: \n, \" and \uXXXX
Last updated: · 6 min read
When text is placed between quotes in a JSON document or in JavaScript source, some characters cannot stay as they are: a double quote would end the string early, a line break would split it, and a backslash changes the meaning of whatever follows. These characters are written as escape sequences, short codes that start with a backslash, such as \n, \t, \", \\ and \uXXXX. JSON defines its rules in RFC 8259, and JavaScript adds a few forms of its own. This guide explains which characters must be escaped, how non-ASCII letters and emoji are written, and what goes wrong when unescaping. The values in the example are real output from the String Escape / Unescape Tool.
Formula
Escape (JSON / JavaScript):
" → \" \ → \\
line feed → \n tab → \t carriage return → \r backspace → \b form feed → \f
other control characters (U+0000–U+001F) → \u00XX
every other character is left unchanged
Non-ASCII to \uXXXX:
U+0080–U+FFFF → \uXXXX (four hex digits)
above U+FFFF → surrogate pair: c' = c − 0x10000; high = 0xD800 + (c' >> 10), low = 0xDC00 + (c' & 0x3FF)
Unescape (JSON / JavaScript):
\n \t \r \b \f \v \0 \" \' \\ \/ \xHH \uXXXX \u{…}RFC 8259 requires three things to be escaped inside a JSON string: the double quote, the backslash and the control characters U+0000 to U+001F. Everything else, accented letters and emoji included, may appear literally. The first operation applies exactly this rule and returns the result without surrounding quotes. The third operation does a different job: it leaves quotes and line breaks alone and only rewrites non-ASCII characters as \uXXXX. Because \u takes four hex digits, code points above U+FFFF (most emoji) are written as two sequences, a UTF-16 surrogate pair. The two decoding operations reverse these steps; the second one also understands the JavaScript-only forms \xHH, \v, \0, \' and \u{…}, while the fourth decodes \u sequences and nothing else.
What the inputs mean
- Text
- The text to convert. When escaping, paste the raw text; when unescaping, paste the part of the string between the quotes. Spaces and line breaks are not trimmed.
- Operation
- Four choices: Escape (JSON / JavaScript), Unescape (JSON / JavaScript), Non-ASCII to \uXXXX and Decode \uXXXX sequences. The first two are inverses of each other, as are the last two.
Worked example: Escaping a two-line text and decoding it again
- The input has two lines. The first is Line 1; the second consists of Tab:, a tab character and then "quoted" \ café 😀. Operation: Escape (JSON / JavaScript).
- The line break becomes \n, the tab \t, each double quote \" and the backslash \\. The é and the 😀 are not control characters and stay as they are.
- The output is a single line: Line 1\nTab:\t\"quoted\" \\ café 😀. Put a double quote before and after it and you have a valid JSON string.
- Feed that output to Unescape (JSON / JavaScript) and the original two-line text comes back unchanged.
- Non-ASCII to \uXXXX: the input café 😀 gives caf\u00e9 \ud83d\ude00. The letter é is U+00E9; 😀 is U+1F600 and is therefore split into the surrogate pair D83D and DE00.
- Decode \uXXXX sequences turns caf\u00e9 \ud83d\ude00 back into café 😀 and touches nothing else: \n caf\u00e9 becomes \n café, with the \n still written as two characters.
- Unescape also accepts JavaScript-only forms: \x41\u{1F600} gives A😀.
- A failing input: pass C:\Users\qa to Unescape and the tool reports Invalid escape sequence (position 3): \Users, because \U is not a valid escape. Running Escape on the same text gives the correct spelling, C:\\Users\\qa.
Result: Escaped form: Line 1\nTab:\t\"quoted\" \\ café 😀. For ASCII-only output, café 😀 → caf\u00e9 \ud83d\ude00.
🔣 String Escape / Unescape Tool
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
Escaping is mostly needed when you place text by hand into a JSON file, a configuration file, test data or a string literal in code. Unescaping helps when logs and API responses are full of \n and \u00e9 and you want to read them. Inside a program, do not build JSON by concatenating strings; use the language's own serialiser (JSON.stringify in JavaScript, json.dumps in Python) and keep this tool for one-off jobs and for checking. JSON escaping covers JSON and JavaScript strings only: HTML, SQL, shell commands and URLs each have their own rules. Writing non-ASCII characters as \uXXXX is optional, because JSON is exchanged as UTF-8; the form is mainly useful for legacy systems that accept only ASCII and for channels where the encoding might get mangled. The conversion runs in your browser and the text is not sent to a server.
Common mistakes
- Writing Windows paths without escaping. In C:\temp\new the \t is decoded as a tab and the \n as a line feed, with no error at all, while a path like C:\Users raises an invalid escape error. Every backslash in a path must be written as \\.
- Escaping twice. Escape an already escaped text and \n turns into \\n; after one round of decoding you are left with a visible \n instead of a line break.
- Treating JSON escaping as enough for HTML or SQL. The escape operation does not touch single quotes, slashes or the less-than sign; text containing </script> needs the escaping of its own context before it goes into a page or a query.
- Using JavaScript-only escapes in JSON. \x41, \v, \0, \' and \u{1F600} are valid in JavaScript but not in JSON, and JSON.parse rejects them.
- Trying to write an emoji as a single \uXXXX. Characters above U+FFFF need a surrogate pair of two sequences; copying only half of the pair produces an invalid character.
- Forgetting about the surrounding quotes. The tool returns the result without quotes: you add them when placing the string in code, and you paste only what is between them when unescaping.
- Overlooking single quotes in a single-quoted JavaScript string. The escape operation targets double-quoted strings; if the result goes between single quotes, each ' in the text must additionally become \'.
Frequently asked questions
Which characters must be escaped in JSON?
According to RFC 8259: the double quote (\"), the backslash (\\) and the control characters U+0000 to U+001F. Control characters without a short form such as \n or \t are written as \u00XX.
Do I have to escape the forward slash?
No. JSON allows \/ but does not require it. The tool leaves slashes alone when escaping and reads \/ as / when unescaping.
Do I need to write accented characters as \uXXXX?
No. JSON text is exchanged as UTF-8, so é, ü or ñ can be written directly. A spelling such as \u00e9 stands for the same character and is only preferred where output is restricted to ASCII.
Why does 😀 become two sequences, \ud83d\ude00?
A \u sequence takes four hex digits and reaches U+FFFF at most. U+1F600 lies above that, so it is split into a high and a low surrogate, as in UTF-16. JavaScript also accepts \u{1F600} for the same character, but JSON does not.
What is the difference between \u00e9 and \xe9?
Both produce é in JavaScript. \xHH has two digits, only reaches U+00FF and is invalid in JSON, whereas \uXXXX works in both.
Is the escape operation the same as JSON.stringify?
Yes, it applies the same rules to a string. The only difference is that the leading and trailing double quotes are removed.
Is my text sent to a server?
No. The conversion is done by JavaScript on the page, in your browser.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.