How to Escape JSON Strings
JSON requires seven characters to be escaped inside a string: the double quote, the backslash, and the control characters for backspace, form feed, newline, carriage return and tab. Anything below U+0020 must use a \u escape. Escaping matters most when embedding JSON inside JSON, where every backslash doubles at each level of nesting.
JSON Escape and Unescape
Escape text for use inside a JSON string, or decode it back.
Escaping is usually invisible because a serialiser does it for you. It becomes visible at exactly two moments: when you hand-write a JSON string containing a quote, and when a payload has to carry another JSON document inside one of its values.
The second case is where the confusion lives. Each level of embedding doubles the backslashes, so a quote that is already escaped once appears as three characters at the next level and seven at the one after.
Step by step
-
Escape the two structural characters
A double quote inside a string becomes \" and a backslash becomes \\. These two are mandatory, because both would otherwise change where the parser thinks the string ends.
-
Escape control characters by name
Newline is \n, carriage return \r, tab \t, backspace \b and form feed \f. A literal newline inside a JSON string is invalid, which is why a multi-line value has to be escaped rather than wrapped.
-
Use \u for anything else below U+0020
Control characters without a shorthand must be written as \u followed by four hex digits — a null byte is \u0000. Ordinary text above U+0020 never needs escaping in a UTF-8 document, including accented letters and emoji.
-
Escape the forward slash only when you want to
JSON permits \/ but does not require it. The convention exists so that </script> cannot appear inside JSON embedded in an HTML page. Escaping it is harmless; not escaping it is equally valid.
-
Escape once per level when nesting
To put a JSON document inside a JSON string value, escape the whole thing as a unit. {"a":"b"} becomes "{\"a\":\"b\"}". Doing it twice by hand is where the backslash counts stop making sense — let a tool handle each level.
Example
The same object, then that object embedded as a string value inside another.
Original
{"name":"Ada","note":"He said \"hi\""}
Embedded as a string
{
"payload": "{\"name\":\"Ada\",\"note\":\"He said \\\"hi\\\"\"}"
}
Frequently asked questions
Which characters must be escaped in JSON?
Do I need to escape single quotes?
Why do the backslashes keep doubling?
Do accented characters and emoji need escaping?
Should I escape forward slashes?
Tools used in this guide
All tools →- JSON Escape and Unescape Escape text for use inside a JSON string, or decode it back. In your browser
- JSON Formatter Beautify, validate and sort JSON with precise error messages. In your browser
- JSON Validator Check whether JSON is valid and see exactly where it breaks. In your browser
- Unicode Escape and Unescape Convert characters to \uXXXX escapes and back. In your browser
Related guides
All guides →- How to Format JSON Turn minified or broken JSON into something readable, and fix it when it will not parse.
- How to Fix "Unexpected Token" JSON Errors What each JSON.parse error message actually means, and why the problem is usually the response rather than the parser.
- UTF-8 vs UTF-16 Two encodings of the same character set, and why string lengths disagree between languages.
Last reviewed .