Invalid escape sequence in JSON: Fix JSON.parse() in JavaScript
JSON.parse() rejects a backslash that is not followed by a valid escape character. Learn why Windows paths, regex and \x escapes trigger it and how to…
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →This error means a string in your JSON contains a backslash that is not followed by a character JSON allows. JSON.parse() stops at that backslash and throws a SyntaxError.
What “Invalid escape sequence in JSON” means
The parser found a backslash inside a string and the next character was not a legal escape. JSON allows only \" \\ \/ \b \f \n \r \t and \u followed by four hex digits. Anything else is invalid. In the broken example, \U is not a JSON escape, so parsing fails at that spot. Engines word the message differently, but the cause is the same.
Causes and the fix for each
Windows file paths with single backslashes
Each backslash in a path starts an escape. \U, \b-style letters and others that are not valid make the parse fail.
Before (fails):{"path": "C:\Users\bob"}
After (valid):
{"path": "C:\\Users\\bob"}
Regex patterns stored in a string
A pattern like \d is a regex token, not a JSON escape. The backslash has to be doubled.
Before (fails):{"re": "\d+"}
After (valid):
{"re": "\\d+"}
Escaped single quote
JavaScript and Python string literals accept \' but JSON does not. A single quote inside a double-quoted JSON string needs no escape.
Before (fails):{"msg": "it\'s"}
After (valid):
{"msg": "it's"}
Hex escape with \x
\x41 is valid in a JavaScript string literal but not in JSON. JSON uses \u with four hex digits, or the plain character.
Before (fails):{"c": "\x41"}
After (valid):
{"c": "\u0041"}
Short or malformed \u escape
\u must be followed by exactly four hex digits. Fewer digits, or non-hex characters, fail the parse.
Before (fails):{"c": "\u41"}
After (valid):
{"c": "\u0041"}
How to fix it in JavaScript
- Read the position in the error message. V8 reports a character offset, so slice a few characters around it and look for the backslash.
- Check what follows that backslash. If it is not one of " \ / b f n r t or u plus four hex digits, it is invalid.
- Double the backslash when you mean a literal one. A path becomes C:\\Users and a regex \d becomes \\d.
- Stop building JSON by hand with string concatenation or template literals. Create the text with JSON.stringify(), which escapes backslashes for you.
- If the text comes from another system and you cannot change it, paste it into the Repair tool or check it in the Validator to see and fix the bad escapes.
- If you parse the same text twice, check for double encoding. A string that was already stringified and then wrapped again can carry stray backslashes.
function parseWithEscapeFix(text) {
try {
return JSON.parse(text);
} catch (err) {
if (!(err instanceof SyntaxError)) throw err;
// Keep valid escapes, double any other lone backslash.
const fixed = text.replace(
/\\(["\\/bfnrt]|u[0-9a-fA-F]{4})|\\/g,
(match, ok) => (ok ? match : "\\\\")
);
return JSON.parse(fixed);
}
}
// Prefer producing valid JSON in the first place:
const safe = JSON.stringify({ path: "C:\\Users\\bob" });
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Bad escaped character in JSON at position 9 | V8 (Chrome, Node.js): backslash followed by an invalid character | Double the backslash or remove the escape |
| JSON.parse: bad escape character at line 2 column 11 of the JSON data | Firefox: same invalid escape | Find the backslash at that line and column and correct it |
| JSON Parse error: Invalid escape character U | Safari: the letter after the backslash is not a valid escape | Replace \U with \\U if the backslash is literal |
| Bad Unicode escape in JSON at position 12 | \u not followed by four hex digits | Pad to four hex digits, such as \u0041 |
| JSON.parse: bad Unicode escape | Firefox: malformed \u sequence | Use exactly four hex digits after \u |
Related errors
- JavaScript JSON.parse() Errors: What They Mean and How to Fix Them
- Invalid JSON Meaning: What It Is and How to Fix It
- Unterminated string in JSON: Fix JSON.parse() in JavaScript
- [Fix JSON Single Quote Errors Instantly — Free Online Tool [2026]](/blog/fix-json-single-quotes)
Key takeaways
- A backslash in JSON must be followed by one of " \ / b f n r t or u plus four hex digits.
- Windows paths and regex patterns are the usual sources of this error.
- Double the backslash to keep a literal one.
- Build JSON with JSON.stringify() instead of by hand.
- Browsers word the message differently, but the cause is the same.
FAQ
Why does JSON.parse accept my string in a JavaScript file but fail at runtime?
A JavaScript string literal is read by the language first, which removes one level of backslashes. The text JSON.parse receives is what remains, and that text must be valid JSON on its own.
Is \/ a valid escape in JSON?
Yes. The standard lists \/ as a valid escape for the forward slash. A plain / also works.
Can I use \x41 in JSON?
No. JSON has no \x escape. Use \u0041 or write the character directly.
Will my regex fix break strings that are already valid?
Not if the pattern consumes valid escape pairs first, as in the code block above. A naive replace of every backslash would double backslashes that were already correct.
Still dealing with broken JSON?
Paste it in and get it fixed in under 1 second — free, no signup, no install. Works with ChatGPT, Claude, n8n, and any AI output.
Fix My JSON Free →Related Articles