JSON String Escaping: Special Characters Guide
JSON strings look simple until you need to include a quote, backslash, or newline. This guide covers every escape sequence with examples and common pitfalls.
How JSON Strings Work
JSON strings are sequences of Unicode characters wrapped in double quotes. Single quotes are never valid. Most characters can appear directly inside the quotes, but a few special characters must be escaped with a backslash (\) to be included in a JSON string.
Complete Escape Sequence Reference
JSON supports exactly these escape sequences:
| Escape Sequence | Character | Description |
|---|---|---|
\" | " | Double quote |
\\ | \ | Backslash |
\/ | / | Forward slash (optional escape) |
\\b | — | Backspace |
\\f | — | Form feed |
\\n | — | Newline (line feed) |
\\r | — | Carriage return |
\\t | — | Horizontal tab |
\\uXXXX | — | Unicode character (4 hex digits) |
Practical Escape Examples
Here's a JSON object demonstrating each escape sequence in context:
{
"quote": "She said, \"Hello, world!\"",
"path": "C:\\Users\\Documents\\file.txt",
"url": "https:\/\/example.com\/api",
"newline": "Line one\nLine two\nLine three",
"tab": "Column1\tColumn2\tColumn3",
"carriageReturn": "Before\r\nAfter",
"unicode": "Caf\u00E9",
"emoji": "Hello \uD83D\uDE00",
"copyright": "\u00A9 2025 JSONTech"
}Escaping Quotes
Since JSON strings are delimited by double quotes, any double quote inside the string must be escaped:
{
"dialogue": "The teacher asked, \"What is JSON?\"",
"html": "<div class=\"container\">content</div>",
"jsonInJson": "{\"key\": \"value\"}"
}The last example — embedding JSON inside a JSON string — is common when storing serialized data. Every inner quote needs escaping, which can make deeply embedded JSON very hard to read.
Escaping Backslashes
Because the backslash is the escape character itself, a literal backslash must be doubled:
{
"windowsPath": "C:\\Program Files\\MyApp\\config.json",
"regex": "\\d+\\.\\d+",
"networkShare": "\\\\server\\share\\folder"
}Windows file paths are the most common source of backslash confusion. Each single \ in the actual path becomes \\ in JSON.
Newlines and Multi-Line Content
JSON strings cannot span multiple lines. A literal line break inside a string is invalid. You must use \n for newlines:
{
"poem": "Roses are red,\nViolets are blue,\nJSON is great,\nAnd so are you.",
"address": "123 Main Street\nApartment 4B\nNew York, NY 10001",
"code": "function hello() {\n console.log(\"Hi\");\n}"
}When this JSON is parsed, the \n sequences become actual newline characters in the resulting string.
Unicode Escape Sequences
The \uXXXX syntax lets you include any Unicode character using its four-digit hexadecimal code point:
{
"cafe": "Caf\u00E9",
"degree": "72\u00B0F",
"copyright": "\u00A9 2025",
"yen": "\u00A5500",
"greek": "\u03B1\u03B2\u03B3",
"checkmark": "\u2713 Complete"
}Characters outside the Basic Multilingual Plane (like many emoji) require a surrogate pair — two \uXXXX sequences. For example, the grinning face emoji (U+1F600) is encoded as \uD83D\uDE00.
Characters That Don't Need Escaping
Most printable Unicode characters can appear directly in JSON strings without escaping. You can include accented letters, CJK characters, and even emoji directly:
{
"french": "Crème brûlée",
"japanese": "東京タワー",
"emoji": "Hello 👋 World 🌍",
"math": "π ≈ 3.14159"
}Using characters directly (rather than \u escapes) is preferred when the file encoding is UTF-8, which is the standard for JSON.
Common Mistakes to Avoid
- Literal newlines in strings: JSON strings must be on a single line. Use
\ninstead of pressing Enter inside a string value. - Single backslash:
"path": "C:\Users"is invalid because\Uis not a recognized escape sequence. Use\\for each backslash. - Using single quotes:
'hello'is invalid JSON. Always use double quotes for strings. - Incomplete Unicode sequences:
\u00Eis missing the fourth hex digit. All Unicode escapes must be exactly four hex digits:\u00E9. - Control characters without escaping: Characters with code points below U+0020 (like tab or newline) must be escaped. They cannot appear literally in JSON strings.
- Confusing JSON escaping with language escaping:When constructing JSON in code, remember that string literals in your language also interpret escape sequences. Use your language's JSON serializer instead of building strings manually.
Use a Serializer, Not String Concatenation
The safest way to produce valid JSON strings is to let your programming language's built-in serializer handle escaping:
// JavaScript
JSON.stringify({ message: 'She said "hello"' })
// → '{"message":"She said \"hello\""}'
// Python
import json
json.dumps({"path": "C:\Users\file.txt"})
# → '{"path": "C:\\Users\\file.txt"}'Manual string building is error-prone and the leading cause of invalid JSON. Let the serializer handle the escaping for you.
Try it yourself: Paste your JSON with special characters into our JSON Formatter to verify the escaping is correct.