Escape special characters in JSON string literals
Which characters must be escaped in a JSON string, how Unicode escapes and surrogate pairs work, and when escaped text hurts readability or usability.
What escaping does to a JSON string
A JSON string literal is text wrapped in double quotation marks. Some characters cannot appear inside it as they are, because a parser would read them as the end of the string or reject them as invalid input. Escaping replaces those characters with backslash sequences, and a JSON parser turns each sequence back into the original character. The text you store or transmit changes, but the value the parser recovers does not.
This changes the stored characters. It is not a visual effect. Escaping is unrelated to reversed or mirrored text, decorative Unicode "fonts", CSS styling or bidirectional display controls. It does not change how the decoded text looks. It only changes how the text is written inside JSON source.
Which characters must be escaped
The JSON specifications (RFC 8259 and ECMA-404) treat only a small set of characters as special. Three groups must be escaped inside a string:
• the double quotation mark
• the backslash (reverse solidus)
• the control characters from U+0000 through U+001F, which include newline, carriage return and tab
Any other character may appear literally if the document is correctly encoded. RFC 8259 requires UTF-8 for JSON exchanged between systems that are not part of a closed ecosystem.
| Character | Escape sequence | Required |
| Double quotation mark (") | " | Yes |
| Backslash () | \ | Yes |
| Newline (U+000A) | \n | Yes |
| Carriage return (U+000D) | \r | Yes |
| Tab (U+0009) | \t | Yes |
| Backspace (U+0008) | \b | Yes |
| Form feed (U+000C) | \f | Yes |
| Other control characters (U+0000 to U+001F) | \u0000 to \u001F | Yes |
| Forward slash (/) | / | No, optional |
| Any other character | \uXXXX (four hex digits) | No, optional |
The \uXXXX form takes exactly four hexadecimal digits. The digits are case-insensitive, so \u00e9 and \u00E9 mean the same thing. JSON has no ' escape and does not accept single-quoted strings, so a single quote inside a JSON string is written as is. Using ' or any other escape not in the table, such as \x41, makes the document invalid.
A worked example
Suppose the original value is two lines of text. The first line holds a Windows-style path and the second holds a quoted reply.
Path: C:\temp
She said "ok"
Written as a JSON string literal, the backslash is doubled, the line break becomes \n, and each inner quotation mark gets a backslash in front of it:
"Path: C:\\temp\nShe said \"ok\""
A JSON parser reading that literal returns exactly the two original lines. The line break has to be escaped. A raw newline inside the quotation marks is a control character and makes the string invalid.
Non-ASCII text, emoji and combining marks
Characters outside ASCII do not need escaping in a UTF-8 JSON document. Some serializers still write them as \u escapes to produce ASCII-only output. Python's json module does this by default, because its ensure_ascii option is on unless you turn it off. JavaScript's JSON.stringify leaves most of these characters as literal text. Both forms decode to the same value.
Characters above U+FFFF, including most emoji, need two \u escapes in escaped form, because JSON represents them as UTF-16 surrogate pairs. For example, the grinning-face emoji, U+1F600, becomes \uD83D\uDE00. Some emoji are built from several code points, such as sequences joined with a zero-width joiner or emoji with a skin-tone modifier. These become several escape sequences, one or two for each code point.
Literal: "café 😀"
ASCII-only: "caf\u00e9 \uD83D\uDE00"
Escaping keeps code points exactly as they are and does not normalize them. Suppose é was typed as a plain e followed by the combining acute accent (U+0301). Its ASCII-only form is then "cafe\u0301", not "caf\u00e9". The two look identical on screen but compare as different strings. Escaping will not fix mismatches caused by different Unicode normalization.
A lone surrogate is half of a surrogate pair with no partner, such as \uD800 on its own. The JSON grammar accepts it, but it does not represent a valid Unicode character. RFC 8259 warns that software handles such strings unpredictably. Treat a lone surrogate as an error to fix, not as something to escape around.
Will escaped text stay usable after copying or sharing?
Escaped text is meant to be read by a parser. Pasting it into anything parsed as JSON works as intended, such as a JSON file, an API request body or a configuration value. Pasting the same characters where they are treated as ordinary text, such as an email, chat message, document or form field, shows the backslashes and \u codes literally. Nothing in those places converts them back.
- Double escaping: escaping a string that is already escaped turns \n into \n and " into \". The decoded value then contains literal backslashes. Escape the raw value once, at the point where it goes into JSON.
- Search and editing: searching for café will not match raw JSON that stores the word as caf\u00e9. Editing long \u sequences by hand is also error-prone. Search and edit the decoded value where you can.
- Accessibility: escape sequences are not meant for people. If escaped text is shown directly, screen readers and other assistive technology will meet backslashes and hex codes instead of the intended words. Text shown to readers should always be the decoded value.
- Other contexts: JSON escaping only makes text safe inside a JSON string. HTML, shell commands, SQL and JavaScript source files each have their own escaping rules, and JSON escaping does not cover them.
Two context-specific cases are worth knowing. First, when JSON is embedded in an HTML script element, a literal </script> inside a string can end the element early. You can avoid this by escaping the slash as / or the angle brackets as \u003C and \u003E. Neither change affects the decoded value. Second, the line separator (U+2028) and paragraph separator (U+2029) are legal unescaped in JSON. They were not legal in JavaScript string literals before ES2019, so escaping them helps if the JSON text will be pasted into older JavaScript source.
Is escaping the right choice for your use?
Escape text when it is going into a JSON string literal, such as an API payload, a stored JSON document, a config file or a test fixture. The benefit is that the value survives parsing exactly, including quotation marks, backslashes and line breaks. The cost is readability. Escaped text is harder to scan, search and edit, and it means nothing outside a JSON parser.
Use plain text when the destination is a person or a system that does not parse JSON. That includes messages, documents, user interfaces, logs meant for reading, and anything a screen reader will present. Keep the decoded value as the source of truth and generate the escaped form only when you serialize. A standard JSON library is safer for this than hand-written replacements. Minimal escaping, meaning only the required characters plus UTF-8 for everything else, is usually the most readable. ASCII-only escaping is useful when a pipeline cannot be trusted to handle UTF-8 correctly.
Gaps in this review
No specific escaping tool, website or library version was tested for this article. It makes no claims about the accuracy, privacy or data handling of any particular online JSON escaper. The behavior described here comes from the JSON specification and documented language behavior. If you rely on a specific tool, parse its output back and compare the result with the original value. Also test how your intended destination displays the result.