JSON Escape Characters and Quotation Rules

Which characters JSON requires you to escape, how double and single quotes work, how Unicode escapes behave, and when escaped text should be replaced with plain text.

JSON escaping is a serialization step. It rewrites certain characters inside a string so a parser can tell exactly where the string starts and ends and what it contains. When a conforming parser reads the result, it gets back the original characters. This review covers which characters must be escaped, how quotation marks work, and when escaped text is or isn't the right form to hand to people or other tools.

Short answer: when escaping is the right choice

Escaping is required whenever text goes inside a JSON string value or object key. It is what makes the document parseable. The main benefit is round-trip fidelity: any Unicode text, including quotes, backslashes and line breaks, survives transport and decodes to the same characters. The decisive tradeoff is readability. The escaped form is written for parsers, not people, and dense runs of backslashes and hex codes are hard to read, edit and search. Use plain text when the audience is a human reader, or when the destination is not a JSON parser, such as a document, a chat message or a log meant for reading.

What escaping actually changes

Escaping changes the stored characters of the serialized JSON text, not their appearance. It has nothing to do with reversed text, mirrored glyphs, decorative Unicode "fonts", CSS styling or bidirectional display. Nothing is reordered, flipped or swapped for a lookalike. For example, a line break inside a value becomes two characters in the file, a backslash followed by n, and a parser turns those two characters back into one line break. The program sees the same value that went in. Only the on-the-wire spelling differs.

Value (as the program holds it):
She said "hi"
C:\temp

Serialized JSON string:
"She said \"hi\"\nC:\\temp"

In the serialized form, the outer double quotes mark the string's boundaries. Inside it, " stands for a literal quotation mark, \n for the line break and \ for a single backslash. Decoding reverses each substitution and nothing else.

Characters JSON requires you to escape

Inside a JSON string, three groups of characters must be escaped:
• the quotation mark
• the backslash (reverse solidus)
• the control characters U+0000 through U+001F, which include tab, line feed and carriage return
Everything else may appear literally, including accented letters and emoji, as long as the document's encoding is handled correctly.

Escape | Represents | Required?
" | quotation mark (U+0022) | Yes, inside a string
\ | backslash (U+005C) | Yes
/ | forward slash (U+002F) | No, optional
\b | backspace (U+0008) | Yes, as \b or \u0008
\f | form feed (U+000C) | Yes, as \f or \u000C
\n | line feed (U+000A) | Yes, as \n or \u000A
\r | carriage return (U+000D) | Yes, as \r or \u000D
\t | tab (U+0009) | Yes, as \t or \u0009
\uXXXX | one UTF-16 code unit as four hex digits | Required for other control characters; optional for everything else

That list is complete. Escapes familiar from other languages, such as ', \a, \v, \x41 or \0, are not valid JSON, and a strict parser will reject a document that contains them. The optional / escape is mainly used when JSON is embedded in an HTML script element, so the output never contains the character sequence that would close the element early. Parsers accept either form of the slash.

Quotation rules: double quotes and single quotes

JSON strings and object keys must be enclosed in double quotes. Single quotes cannot delimit strings, so {'name': 'Ana'} is invalid JSON. Some lenient parsers accept it, and so do JavaScript object literals, but neither makes it valid JSON.

To include a double quote in a value, write it as ". To include a single quote or apostrophe, write it as-is. Single quotes have no special meaning in JSON, so they need no escaping, and ' is itself an error. If you see ' in what looks like JSON, it was probably produced by another language's escaping rules. It may also come from escaping for a surrounding context, such as a SQL statement or shell command, rather than for JSON.

Valid:   {"quote": "It's a \"test\""}
Invalid: {'quote': 'It\'s a "test"'}
Invalid: {"quote": "It\'s a test"}

A single quote can also be written as \u0027. This is valid but unnecessary within JSON itself. It is sometimes used when JSON is placed inside a single-quoted attribute or string in another language, in which case the escaping serves that outer context, not JSON.

Unicode escapes, emoji and combining marks

A \uXXXX escape encodes one UTF-16 code unit. Characters outside the Basic Multilingual Plane, including most emoji, need two escapes that form a surrogate pair. For example, U+1F600 (grinning face) becomes \uD83D\uDE00. The two halves must stay together and in order. Splitting them, cutting a string between them or emitting one alone produces an unpaired surrogate, which the standard warns can lead to unpredictable behavior across implementations.

Escaping works one code point at a time and never normalizes or combines characters. An "é" stored as a single code point escapes as \u00e9. The same letter stored as "e" followed by a combining acute accent escapes as e\u0301. Both decode to text that looks identical, but they are different strings, so equality checks and searches may treat them differently. Likewise, an emoji built from several code points joined by zero-width joiners escapes to a long run of \u sequences. It decodes back to the original only if the whole run is kept intact.

"\u00e9"          → é  (one code point)
"e\u0301"         → é  (e + combining acute accent)
"\uD83D\uDE00"    → 😀 (U+1F600 as a surrogate pair)

RFC 8259 requires UTF-8 for JSON exchanged between systems that are not part of a closed ecosystem. Under UTF-8, non-ASCII characters can be written literally, so escaping them is a choice rather than a rule. It helps when part of a pipeline might mangle non-ASCII bytes, at a cost to readability.

Common mistakes that change meaning

  • Double escaping: Escaping text that is already escaped turns \n into \n. The decoded value then contains a literal backslash and the letter n instead of a line break. This often happens when JSON is built by string concatenation and then passed through a serializer again.
  • Hand-assembling JSON: Placing user text between quotes without escaping it lets a stray quote or backslash end the string early or break the document. A language's built-in JSON serializer handles escaping reliably.
  • Mixing layers: Shell quoting, SQL string literals, HTML attributes and regular expressions each have their own escaping rules, applied on top of JSON's. Escape for each layer from the innermost outward, and decode in the reverse order.
  • Raw line breaks: Pasting multi-line text directly between quotes produces invalid JSON. Line breaks inside a string must be written as escapes.
  • U+2028 and U+2029: The line separator and paragraph separator are legal unescaped in JSON, but JavaScript string literals did not allow them before ES2019. Escaping them as \u2028 and \u2029 avoids errors when JSON is pasted into older JavaScript source.

Will escaped text stay usable after copying or sharing?

Escaped JSON generally copies cleanly between editors, terminals and APIs that treat it as raw text. The risks come from destinations that are not JSON-aware, or from cases where the reader is a person.

  • Typographic substitution: Some word processors, note apps and messaging apps can replace straight quotes with curly ones, or alter whitespace, when text is pasted. A curly quote is not a JSON delimiter, so the result may no longer parse.
  • Accessibility: Escape sequences are just characters. A screen reader would be expected to announce backslashes and hex digits rather than the intended punctuation or emoji. When a person is the audience, provide the decoded text.
  • Search and editing: Searching raw JSON for "café" will not match "caf\u00e9". Editing inside escape sequences also makes it easy to break a surrogate pair or drop a backslash. It is safer to decode, edit, then serialize again.
  • Reading order: Escaping does not reorder characters, so right-to-left and mixed-direction text keeps its logical order. How that text displays after decoding depends on the rendering environment, not on JSON.

A plain-text version is needed whenever the output is meant to be read rather than parsed. Examples include documentation prose, support replies, interface labels and accessible content. The exception is code samples meant to be copied into a program. There, the escaped form is correct, and it is best shown in a code block so readers can see it is code rather than prose.

Evidence and gaps

The escape list, quoting rules, surrogate-pair handling and UTF-8 requirement come from the published JSON standard (RFC 8259 and ECMA-404). The point about U+2028 and U+2029 reflects the ECMAScript specification's history. The cautions about clipboards, messaging apps and screen readers are general, not the results of scoped tests on named products, so check the specific destination before relying on them. How lenient particular parser libraries are about single quotes or invalid escapes varies and was not evaluated here.