Blog

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

CharacterEscapeNotes
"\"Double quotes delimit the string
\\\Backslash is the escape character
newline\nLF — raw newlines are invalid
carriage return\rCR
tab\tHorizontal tab
form feed\fRarely needed
backspace\bRarely needed
/\/Optional — JSON allows it either way
Unicode\uXXXXFour hex digits, e.g. A = A

What breaks most often

Unescaped double quotes inside a string value:

json
// 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:

json
// 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:

json
{ "note": "Line one\nLine two\nLine three" }

Programmatic escaping

Never hand-escape JSON you're building in code. Let the language do it:

js
const obj = { message: 'She said "Hello"', path: 'C:\\Users\\alice' };
const json = JSON.stringify(obj);
// {"message":"She said \"Hello\"","path":"C:\\Users\\alice"}
python
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.

Got a file to check?
Paste it in. Errors come back in plain English.
Open Config Validator →