JSON strings have strict rules about which characters must be escaped. A single unescaped backslash or literal newline inside a string will cause a parse error in every JSON parser. This guide covers every required escape sequence, common mistakes, and how to fix them.
JSON Escape Character Reference Table
The JSON specification (RFC 8259) requires these characters to be escaped inside string values:
| Character | Escaped Form | Example JSON String | Result Value |
|---|---|---|---|
| Double quote " | \" | "say \"hello\"" | say "hello" |
| Backslash \ | \\ | "C:\\Users\\name" | C:\Users\name |
| Forward slash / | \/ | "http:\/\/example.com" | http://example.com |
| Newline | \n | "line1\nline2" | line1↵line2 |
| Carriage return | \r | "line1\r\nline2" | line1⏎line2 |
| Tab | \t | "col1\tcol2" | col1→col2 |
| Form feed | \f | "page1\fpage2" | page1[FF]page2 |
| Backspace | \b | "abc\bx" | abx (erases c) |
| Unicode | \uXXXX | "\u20AC100" | €100 |
Note: Forward slash escaping (\/) is optional in JSON but useful when embedding JSON in HTML to avoid</script> injection.
Common Mistakes
1. Using Single Quotes Instead of Double Quotes
JSON only recognizes double quotes for strings. Single quotes cause an immediate parse error. There is no need to escape single quotes inside JSON strings — they are valid as-is.
Broken:
{'name': 'Alice', 'city': "New York"}
// SyntaxError: Unexpected token 'Fixed — all strings in double quotes:
{"name": "Alice", "city": "New York"}2. Windows File Paths (Unescaped Backslashes)
Windows paths use backslashes. In JSON, every backslash must be doubled. This is the most common source of "Invalid escape sequence" errors in config files.
Broken:
{
"logPath": "C:\Users\alice\logs"
}
// SyntaxError: Invalid escape sequence '\U'Fixed — double every backslash:
{
"logPath": "C:\\Users\\alice\\logs"
}
// Parsed value: C:Usersalicelogs3. Multi-line Strings (Literal Newlines)
JSON strings cannot contain literal newlines (pressing Enter inside the string). Use\n instead.
Broken — literal newline in string:
{
"message": "Hello,
World"
}
// SyntaxError: Unterminated stringFixed — use \\n escape:
{
"message": "Hello,\nWorld"
}
// Parsed value:
// Hello,
// World4. HTML in JSON (Safety Escaping)
When embedding JSON in an HTML page, characters like&,<, and> can cause XSS issues or HTML parsing problems. Escape them as Unicode sequences.
| Character | Safe Unicode Escape |
|---|---|
| & | \u0026 |
| < | \u003C |
| > | \u003E |
Fix: Windows File Paths in JSON
Broken config.json:
{
"outputDir": "C:\Users\alice\Documents\output",
"tempDir": "C:\Temp\myapp"
}Fixed — each backslash doubled:
{
"outputDir": "C:\\Users\\alice\\Documents\\output",
"tempDir": "C:\\Temp\\myapp"
}
// Or use forward slashes — they work on Windows too:
{
"outputDir": "C:/Users/alice/Documents/output",
"tempDir": "C:/Temp/myapp"
}Fix: Newlines in JSON
Broken — literal newline breaks the string:
{
"body": "Dear Alice,
Thank you for your order.
Best regards"
}
// SyntaxError: Unterminated string in JSON at line 2Fixed — use \\n for newlines:
{
"body": "Dear Alice,\n\nThank you for your order.\n\nBest regards"
}JavaScript: JSON.stringify() Handles It For You
Never build JSON strings manually with string concatenation.JSON.stringify()escapes all required characters automatically.
// All escaping handled automatically
const obj = {
path: "C:\Users\alice", // backslash
message: "She said \"hello\"", // double quote
text: "line1\nline2", // newline
note: "tab\there", // tab
};
const json = JSON.stringify(obj, null, 2);
console.log(json);
// {
// "path": "C:\\Users\\alice",
// "message": "She said \"hello\"",
// "text": "line1\nline2",
// "note": "tab\there"
// }
// Safely embed in HTML script tag
const escaped = JSON.stringify(obj)
.replace(/</g, '\u003C')
.replace(/>/g, '\u003E')
.replace(/&/g, '\u0026');Python: json.dumps() Handles It For You
json.dumps() in Python correctly escapes all required characters. Use ensure_ascii=False to keep Unicode characters as-is rather than escaping them as \uXXXX.
import json
data = {
"path": "C:\\Users\\alice", # Python raw string or doubled backslash
"message": 'She said "hello"', # double quotes inside Python string
"text": "line1\nline2", # newline escape
"price": "€100", # Unicode
}
# json.dumps handles all escaping
output = json.dumps(data, indent=2)
print(output)
# {
# "path": "C:\\Users\\alice",
# "message": "She said \"hello\"",
# "text": "line1\nline2",
# "price": "\u20ac100" ← default: ASCII-safe
# }
# Keep Unicode characters readable
output_unicode = json.dumps(data, indent=2, ensure_ascii=False)
# "price": "€100" ← Unicode kept as-isAuto-fix JSON escape errors online
Paste your JSON and our formatter will highlight and fix escape errors instantly.
Frequently Asked Questions
What characters must be escaped in JSON?
The required escapes are: double quote (\"), backslash (\\), and control characters including newline (\n), carriage return (\r), tab (\t), form feed (\f), and backspace (\b).
How do I escape a backslash in JSON?
Double it: use \\ in the JSON text to represent one backslash. Windows paths likeC:\Users\name must be written as "C:\\\\Users\\\\name" in JSON.
Can I use single quotes in JSON?
No. JSON strings must use double quotes. Single quotes cause a parse error. There is no need to escape single quotes — they can appear as literal characters inside a double-quoted JSON string.
How do I include a newline in a JSON string?
Use the escape sequence \n. A literal newline character inside a JSON string is invalid and will cause a parse error. For multi-line text, join all lines with \n within a single string value.
How do I escape Unicode characters in JSON?
Use \uXXXX with the 4-digit hex code point (e.g., \u20AC for €). In practice, JSON.stringify() andjson.dumps() handle this automatically.