How to Escape Special Characters in JSON Strings
JSON strings require backslash escapes for quotes, backslashes, newlines, and control characters. Here's every escape sequence you need.
If your JSON string contains a double quote, a backslash, or a newline, you must escape it — otherwise the parser will reject the document.
The required escape sequences
| Character | Escape | Notes |
|---|---|---|
" | \" | Double quotes delimit the string |
\ | \\ | Backslash is the escape character |
| newline | \n | LF — raw newlines are invalid |
| carriage return | \r | CR |
| tab | \t | Horizontal tab |
| form feed | \f | Rarely needed |
| backspace | \b | Rarely needed |
/ | \/ | Optional — JSON allows it either way |
| Unicode | \uXXXX | Four hex digits, e.g. A = A |
What breaks most often
Unescaped double quotes inside a string value:
// INVALID — the parser sees the string end after "Hello"
{ "message": "She said "Hello" to me" }
// Valid
{ "message": "She said \"Hello\" to me" }
Windows file paths contain backslashes:
// INVALID
{ "path": "C:\Users\alice\file.txt" }
// Valid
{ "path": "C:\\Users\\alice\\file.txt" }
Multi-line strings — JSON has no literal multi-line syntax. Use \n:
{ "note": "Line one\nLine two\nLine three" }
Programmatic escaping
Never hand-escape JSON you're building in code. Let the language do it:
const obj = { message: 'She said "Hello"', path: 'C:\\Users\\alice' };
const json = JSON.stringify(obj);
// {"message":"She said \"Hello\"","path":"C:\\Users\\alice"}
import json
json.dumps({"message": 'She said "Hello"'})
# '{"message": "She said \\"Hello\\""}'
What about control characters?
Any character with a code point below U+0020 (space) must be escaped as \uXXXX. Raw control characters in a JSON string are invalid even if some parsers tolerate them.
Try it
Paste your JSON into the JSON formatter and validator to catch unescaped characters instantly — it highlights the exact line where the syntax breaks.
For the broader picture of what else trips up JSON, see common JSON syntax errors.