Escape JSON String: Special Characters, Backslashes, and Unicode
Last updated:
JSON strings must escape 8 characters per RFC 8259: \" (double quote), \\ (backslash), \b (backspace), \f (form feed), \n (newline), \r (carriage return), \t (tab), and \/ (forward slash — optional). All 32 control characters U+0000–U+001F must be escaped, typically as \uXXXX 4-hex-digit sequences. The safest approach is JSON.stringify(value) in JavaScript or json.dumps(value) in Python — both handle all 8 required sequences plus Unicode automatically. This guide covers each escape sequence with examples, the ensure_ascii=True option for pure-ASCII output, how to unescape a JSON string with JSON.parse('"' + s + '"'), and the 4 most common parse errors caused by bad escaping.
Characters that must be escaped
The JSON specification (RFC 8259) requires these characters to be escaped inside a string value:
| Character | Escape sequence | Notes |
|---|---|---|
Double quote " | \" | Required — terminates the string otherwise |
Backslash \ | \\ | Required — introduces escape sequences |
| Newline | \n | U+000A |
| Carriage return | \r | U+000D |
| Tab | \t | U+0009 |
| Backspace | \b | U+0008 |
| Form feed | \f | U+000C |
| Any control char | \uXXXX | U+0000–U+001F |
Forward slash / may be escaped as \/ but does not have to be. Unicode characters above U+001F do not need escaping but can be written as \uXXXX if ASCII-only output is required.
Common escaping mistakes
Unescaped backslash in Windows file paths
// ❌ Invalid
{"path": "C:UsersAliceDocuments"}
// ✅ Valid
{"path": "C:\Users\Alice\Documents"}Unescaped double quote inside a string
// ❌ Invalid
{"message": "She said "hello" to me"}
// ✅ Valid
{"message": "She said \"hello\" to me"}Literal newline instead of \n
// ❌ Invalid — actual line break inside the string
{"text": "line one
line two"}
// ✅ Valid
{"text": "line one\nline two"}Let JSON.stringify handle escaping
In JavaScript, JSON.stringify() escapes all required characters automatically. Never build JSON strings by hand.
const path = 'C:\Users\Alice'
const message = 'She said "hello"'
const multiline = 'line one\nline two'
// JSON.stringify escapes everything correctly
const json = JSON.stringify({ path, message, multiline })
console.log(json)
// {"path":"C:\\Users\\Alice","message":"She said \"hello\"","multiline":"line one\\nline two"}See the JSON.stringify tutorial for replacers, spacing, and safe circular reference handling.
Escaping for use inside HTML attributes
When embedding JSON in an HTML attribute or a <script> tag, double-escaping issues can arise. The safest pattern is to serialize server-side and inject into a <script type="application/json"> block rather than an attribute value.
<!-- Preferred: script tag injection -->
<script type="application/json" id="app-config">
{"theme":"dark","locale":"en"}
</script>
<script>
const config = JSON.parse(document.getElementById('app-config').textContent)
</script>Unicode escape sequences
Any Unicode code point can be written as \uXXXX where XXXX is the four-digit hex value. Characters outside the Basic Multilingual Plane (above U+FFFF) require a surrogate pair.
// Unicode escape for a copyright symbol
{"notice": "\u00A9 2024 Acme Corp"}
// Emoji using surrogate pair (U+1F600)
{"emoji": "\uD83D\uDE00"}In practice, modern JSON parsers handle UTF-8 directly and surrogate pairs are rarely needed unless you need pure ASCII output.
Validate your escaped JSON
Paste the JSON into Jsonic's Formatter to confirm the escaping is correct and the string values parse as expected.
Open JSON FormatterFrequently asked questions
Which characters must be escaped in JSON strings?
The double quote (\"), backslash (\\), and all control characters from U+0000 to U+001F. Named escapes include \n, \r,\t, \b, and \f.
How do I escape a backslash in JSON?
Write \\ for a single backslash. Windows paths must double every backslash:C:\\Users\\Alice becomes "C:\\\\Users\\\\Alice" in JSON.JSON.stringify does this automatically.
What is the difference between \n in JSON and a real newline?
\n is the two-character escape sequence for a newline. A literal line break inside a JSON string value is illegal — parsers throw a SyntaxError. Always use the escape sequence.
How do I escape a JSON string in JavaScript?
Call JSON.stringify(yourString). It wraps the string in double quotes and escapes all required characters. To get the escaped content without surrounding quotes, use JSON.stringify(str).slice(1, -1).
What characters are illegal in JSON strings without escaping?
Double quote, backslash, and all control characters U+0000–U+001F — including literal tabs, newlines, and carriage returns. These must use their escape sequences (\t, \n, \r).
How do I unescape a JSON string?
Wrap the escaped string in double quotes and call JSON.parse:JSON.parse('"' + escaped + '"'). For a complete JSON document, callJSON.parse directly — it unescapes all string values during normal parsing.
Recommended reading
- Designing Data-Intensive Applications (2nd Edition) — Martin Kleppmann & Chris RiccominiThe modern classic on data systems — encoding formats, schemas, replication, and stream processing.
- JavaScript: The Definitive Guide (7th Edition) — David FlanaganThe complete reference for the language JSON came from — serialization, async, and the full standard library.
As an Amazon Associate, Jsonic earns from qualifying purchases.