How To Escape A JSON String
Learn which characters JSON strings cannot contain, how backslash escape sequences work, and how to escape and unescape text safely.
Quick Answer
JSON escaping replaces the characters a JSON string literal cannot contain, quotes,
backslashes, and control characters like newlines, with backslash sequences such as
\", \\, and \n. To escape text for JSON, paste it
and copy the escaped result; to unescape a JSON string, paste the escaped form and the
original text comes back. The tool detects the direction automatically and converts live.
How It Works: JSON String Escaping
A JSON string literal sits between double quotes: "like this". The moment the
text inside contains its own double quote, the parser reads it as the end of the string and
the document breaks. Real line breaks and tabs are just as illegal inside a JSON string
literal. JSON string escaping solves this by writing each forbidden character as a
two-character escape sequence that the parser understands: "He said \"hi\"\nand left" stores
a quote-containing, two-line sentence in one legal literal.
Which Characters Must Be Escaped in JSON
The JSON escape characters come in exactly three groups, defined by RFC 8259:
- the double quote
", written\" - the backslash
\, written\\ - every control character U+0000 to U+001F; the common ones have short forms
(
\b \f \n \r \t), the rest use\uXXXX
Everything else, including emoji, accents, and non-Latin scripts, is legal unescaped. However, this tool additionally escapes U+2028 and U+2029 (line and paragraph separators), because JSON allows them raw while JavaScript string literals do not; escaping them makes the output safe to inline into code as well.
JSON Escape vs URL Encode and Other Escapers
Each container has its own escaping rules, and the tools are not interchangeable. JSON
escaping protects text inside a JSON string literal with backslash sequences. URL encoding
protects text inside a URL with percent sequences like %20. HTML entity
encoding protects text inside markup with entities like &. For
example, a JSON payload carried in a query parameter needs both, applied in order: escape
for JSON first, then URL encode the whole document. The switcher above the tool jumps
between all of these encoders with your input preserved.
Escaping also differs from stringifying a document. To escape a JSON string is to protect
one value's text; stringifying serializes a whole object into JSON syntax, quotes included.
JSON.stringify does both at once for programmatic use, whereas this tool
answers the paste-into-an-existing-document case.
Examples
A quoted sentence. He said "hello" escapes to
He said \"hello\". The quotes survive the round trip into a config value.
A Windows path. C:\temp\new escapes to
C:\\temp\\new. Without the doubled backslashes, \t and
\n would silently turn into a tab and a newline when parsed.
A document inside a document. Embedding {"a": "b"} as a
string value produces {\"a\": \"b\"}. Therefore each nesting level
doubles the backslashes, which is the telltale sign of double escaping.
How to Unescape a JSON String
JSON unescape is the reverse direction. Paste the escaped form, with or without the outer
quotes, and the tool parses it back to plain text: \n becomes a real line break, \uXXXX becomes the
character it names. Invalid input, for example a dangling backslash or a bad
\u sequence, is reported as an error instead of being silently mangled. The
direction detector treats input containing escape sequences or wrapped in quotes as an
escaped string, and you can override it with the mode select at any time.
Common Mistakes
Related Tools
You May Also Need
You may also need
- JSON ValidatorVerify the document parses after pasting the escaped value
- JSON FormatterPretty-print the document the escaped string lands in
Next steps
- JSON ValidatorCheck the full document after embedding the string
- JSON FormatterFormat the resulting document for review
Alternatives
- URL Encoder & DecoderEscape for URLs instead of JSON string literals