Invalid escape sequence in JSON: How to Fix It in Python
Python raises Invalid \escape when a JSON string has a backslash followed by a character JSON does not allow. See the causes, the fix steps and a code…
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →Python's json module raises this error when a string contains a backslash followed by a character that JSON does not define as an escape. The usual fix is to double the backslash, or to remove it when it does nothing.
What “Invalid escape sequence in JSON” means
The parser found a backslash inside a string and the next character is not a valid escape. JSON allows only \" \\ \/ \b \f \n \r \t and \u followed by four hex digits. Anything else is rejected. Python reports it as Invalid \escape with the line, column and character offset of the backslash. The offset points at the backslash, not at the end of the string.
Causes and the fix for each
Windows path with single backslashes
\U and \d are not JSON escapes, so a raw Windows path fails.
Before (fails):{"path": "C:\Users\data"}
After (valid):
{"path": "C:\\Users\\data"}
Regex pattern pasted into a string
Patterns such as \d or \s use backslash sequences that JSON does not define.
Before (fails):{"pattern": "\d+\s\w+"}
After (valid):
{"pattern": "\\d+\\s\\w+"}
Escaped single quote
JSON has no \' escape. A single quote needs no escape inside a double-quoted string.
Before (fails):{"text": "it\'s done"}
After (valid):
{"text": "it's done"}
Hex escape copied from another language
JSON has no \x escape. Use \u with four hex digits instead.
Before (fails):{"char": "\x41"}
After (valid):
{"char": "\u0041"}
Incomplete unicode escape
\u needs exactly four hex digits. Python reports this one as Invalid \uXXXX escape.
Before (fails):{"char": "\u41"}
After (valid):
{"char": "\u0041"}
Text built by hand or by an LLM
Code that joins strings without json.dumps, and some model output, can leave single backslashes in the text.
Before (fails):{"note": "line one\ line two"}
After (valid):
{"note": "line one\nline two"}
How to fix it in Python
- Read the error position. Python gives line, column and char for the backslash. Look at that character and the one after it.
- If the backslash is literal, such as a path or a regex, double it so the string holds one backslash after parsing.
- If the backslash does nothing, such as \' , delete it.
- If you produce the JSON yourself, build it with json.dumps on a Python dict instead of joining strings. json.dumps escapes backslashes for you.
- Paste the text into the Validator to find the exact spot, or into the Repair tool to have the bad backslashes doubled for you.
- If the input comes from an API or a model, catch the error and apply a repair step before you retry json.loads.
import json
import re
ESCAPE = re.compile(r'\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})|\\')
def double_bad_backslashes(text):
# Keep valid escapes, double any other lone backslash.
return ESCAPE.sub(
lambda m: m.group(0) if len(m.group(0)) > 1 else "\\\\",
text,
)
def load_json(text):
try:
return json.loads(text)
except json.JSONDecodeError as err:
if "escape" not in err.msg:
raise
return json.loads(double_bad_backslashes(text))
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Invalid \escape: line 2 column 14 (char 15) | Backslash followed by a character JSON does not define, such as \U or \d | Double the backslash or remove it |
| Invalid \uXXXX escape: line 1 column 12 (char 11) | \u followed by fewer than four hex digits | Pad to four hex digits or double the backslash |
| Invalid control character at: line 1 column 10 (char 9) | A raw newline or tab inside a string | Replace it with \n or \t |
| Unterminated string starting at: line 1 column 9 (char 8) | A trailing backslash that escapes the closing quote | Double the final backslash |
| Expecting ',' delimiter: line 1 column 20 (char 19) | An unescaped double quote inside a string | Escape the quote as \" |
Related errors
- Invalid control character in JSON: How to Fix It in Python
- Unterminated string in JSON: How to Fix It in Python
- Fix Python JSON Errors: json.loads() Decode Errors and How to Repair Them
- Invalid JSON Meaning: What It Is and How to Fix It
- JSONDecodeError: Expecting ',' delimiter: How to Fix It in Python
Key takeaways
- Invalid \escape means a backslash is followed by a character JSON does not define.
- Valid escapes are \" \\ \/ \b \f \n \r \t and \uXXXX.
- Windows paths and regex patterns are the usual source.
- Build JSON with json.dumps and the escaping is done for you.
- A backslash that is not part of a valid escape can be doubled in code or with the Repair tool.
FAQ
Which escapes does JSON allow?
JSON allows \" \\ \/ \b \f \n \r \t and \u followed by four hex digits. Every other backslash sequence is invalid.
Why does my path work in Python but fail in json.loads?
Python string literals and JSON have different escape rules. A Python literal may accept a sequence that the JSON parser rejects. The JSON text must have the backslash doubled.
Can I use forward slashes in Windows paths instead?
Yes. Forward slashes need no escaping in JSON, and Windows accepts them in most APIs. Check that the program reading the path accepts them.
Does json.loads have a setting to ignore bad escapes?
No. The standard json module has no option to tolerate them. Fix the text first or use a repair step. The strict=False option only relaxes control characters.
Will doubling every backslash fix it?
Not safely. Doubling valid escapes such as \n changes their meaning. Double only the backslashes that are not part of a valid escape.
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