JSON troubleshooting
Bad escaped character in JSON / Invalid \escape
Fix backslashes JSON cannot read, such as Windows paths, regex patterns and \' escapes. Exact JavaScript, Python, Firefox, Go and .NET messages, with fixes.
What the error means
Inside a JSON string, a backslash must start one of nine escapes: \" \\ \/ \b \f \n \r \t, or \u followed by exactly four hexadecimal digits. Any other character after a backslash is invalid. The usual sources are Windows paths such as C:\Users, regular expressions such as \d, and \' copied from JavaScript or SQL strings. A \u escape without four hex digits has its own Unicode-escape message.
Exact messages by runtime
Messages depend on the input and runtime version. Node and Python rows were reproduced locally. Other rows cite official documentation or source. Rows marked as fragments or templates preserve the source wording and are not complete exception transcripts. Browsers and libraries may append location details.
| Runtime | Message | Input and evidence |
|---|---|---|
| JavaScript / V8 | SyntaxError: Bad escaped character in JSON at position 12 (line 1 column 13) | JSON.parse on {"path":"C:\Users\ada"}Reproduced in Node.js 24.21.0 |
| JavaScript / V8, \u escape | SyntaxError: Bad Unicode escape in JSON at position 3 (line 1 column 4) | JSON.parse on "\uZZZZ"Reproduced in Node.js 24.21.0 |
| Python | json.decoder.JSONDecodeError: Invalid \escape: line 1 column 12 (char 11) | json.loads(r'{"path":"C:\Users\ada"}')Reproduced in Python 3.14.4 |
| Python, \u escape | json.decoder.JSONDecodeError: Invalid \uXXXX escape: line 1 column 3 (char 2) | json.loads(r'"\uZZZZ"')Reproduced in Python 3.14.4 |
| Firefox | SyntaxError: JSON.parse: bad escaped character at line 1 column 13 of the JSON data | The same path inputSource-derived from Firefox 158 JSONParser.cpp: reported at the character after the backslash |
| Safari / JavaScriptCore | JSON Parse error: Invalid escape character U | The same path inputJavaScriptCore source (WebKit tag webkitgtk-2.54.1) with the JSON Parse error prefix |
| Go / encoding/json | invalid character 'U' in string escape code | The same path inputOfficial Go 1.27 scanner source |
| Java / Jackson | Unrecognized character escape 'U' (code 85) | The same path inputSource-derived from Jackson 2.22; location suffix omitted |
| C# / System.Text.Json | 'U' is an invalid escapable character within a JSON string. The string should be correctly escaped. | The same path inputOfficial .NET 10 resource template with U substituted; path and location omitted |
| C# / Json.NET | Bad JSON escape sequence: \U. | The same path inputJson.NET 13.0.4 source template; path, line and position suffix omitted |
| PHP / json_last_error_msg() | Syntax error | The same path inputOfficial PHP 8.5 scanner rule: an invalid escape is JSON_ERROR_SYNTAX |
Causes you can reproduce
A Windows path was written into JSON by hand
In JSON text every backslash that belongs to the value is doubled, as in C:\\Users\\ada. String.raw keeps the single backslashes here, as a file or HTTP body would.
const text = String.raw`{"path":"C:\Users\ada"}`;
JSON.parse(text);
// Bad escaped character in JSON at position 12A regular expression was copied into a value
The pattern \d+ must be written as \\d+ inside a JSON string.
import json
json.loads(r'{"pattern": "\d+"}')
# Invalid \escape: line 1 column 14 (char 13)A \' escape came from JavaScript or SQL
An apostrophe needs no escape inside a double-quoted JSON string. Remove the backslash: O'Brien.
JSON.parse(String.raw`{"name":"O\'Brien"}`);
// Bad escaped character in JSON at position 11How to fix it
- Find the backslash at the reported position. V8 and Firefox point at the character after it; Python points at the backslash itself.
- Decide what the value should contain. A literal backslash, as in a path or a pattern, needs two backslashes in JSON text. A backslash that only escaped an apostrophe in another language should be removed.
- Fix the producer: serialize the original string with JSON.stringify, json.dumps or your platform serializer instead of inserting raw text between quotes.
- Watch the source-code layer too. In JavaScript and Python a backslash in an ordinary string literal is already an escape, so JSON written by hand in code needs String.raw or a Python raw string.
Serialize paths and patterns in JavaScript
The JavaScript literals below contain single backslashes after parsing. JSON.stringify doubles them in the JSON text.
const value = { path: 'C:\\Users\\ada', pattern: '\\d+', name: "O'Brien" };
const text = JSON.stringify(value);
console.log(text); // {"path":"C:\\Users\\ada","pattern":"\\d+","name":"O'Brien"}
console.log(JSON.parse(text).path); // C:\Users\adaSerialize in Python and use raw strings for literals
Raw strings keep backslashes for the value; json.dumps escapes them for JSON.
import json
value = {"path": r"C:\Users\ada", "pattern": r"\d+"}
text = json.dumps(value)
print(text) # {"path": "C:\\Users\\ada", "pattern": "\\d+"}
assert json.loads(text) == valueFind the exact position with Tool Kumar
- Copy the exact input text, including its original whitespace. For HTTP failures, copy the raw response body.
- Open the Tool Kumar JSON validator and paste the text. The document stays in your browser.
- Read the line and column in the error or fixable-issue banner. For an invalid document, select Go to error to move the caret to that location.
- Inspect the preceding token as well as the marked character. A parser marks where it discovered the problem; the missing quote, separator or value can start earlier.
Python character offsets are zero-based, while its displayed lines and columns are one-based. V8 string positions count UTF-16 code units; Go offsets count bytes read. Unicode text can make these offsets differ. Use the original text and its line/column context rather than copying an offset between runtimes.
Tool Kumar can classify some input as fixable loose JSON. Review a proposed change before using the JSON repair tool, then validate the result against your API contract. For empty input, inspect the producer or response because there is no character to select.
Frequently asked questions
Which escapes are valid in JSON?
A backslash followed by a double quote, a backslash, a forward slash, b, f, n, r or t, or by u and four hexadecimal digits. Every other character after a backslash is an error in a strict parser.
Why does \' fail when JavaScript accepts it?
JavaScript string literals allow \' because they can be delimited by single quotes. JSON strings are always double-quoted, so an apostrophe needs no escape and \' is invalid.
Should I replace backslashes with forward slashes?
Only if the receiver treats them as the same thing, because that changes the data. Escaping the backslash keeps the original value.
Does Python strict=False allow invalid escapes?
No. strict=False only allows raw control characters inside strings. An invalid escape such as \U still raises Invalid \escape.
Can Tool Kumar repair invalid escapes?
The validator marks each invalid escape with its position. Its repair removes the backslash and keeps the next character. That is right for \' but turns C:\Users into C:Users, so for paths and patterns fix the producer instead.
Related errors and tools
- Bad control character in string literal in JSON
- Expecting ',' delimiter / Expected ',' or '}' after property value
- Expecting property name enclosed in double quotes
- All JSON error guides
- JSON syntax reference
Standards and diagnostic sources
RFC 8259 and ECMA-404 define JSON syntax. Diagnostic wording belongs to the parser. The table links each cited message to its source.