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.

RuntimeMessageInput and evidence
JavaScript / V8SyntaxError: 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 escapeSyntaxError: Bad Unicode escape in JSON at position 3 (line 1 column 4)JSON.parse on "\uZZZZ"
Reproduced in Node.js 24.21.0
Pythonjson.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 escapejson.decoder.JSONDecodeError: Invalid \uXXXX escape: line 1 column 3 (char 2)json.loads(r'"\uZZZZ"')
Reproduced in Python 3.14.4
FirefoxSyntaxError: JSON.parse: bad escaped character at line 1 column 13 of the JSON dataThe same path input
Source-derived from Firefox 158 JSONParser.cpp: reported at the character after the backslash
Safari / JavaScriptCoreJSON Parse error: Invalid escape character UThe same path input
JavaScriptCore source (WebKit tag webkitgtk-2.54.1) with the JSON Parse error prefix
Go / encoding/jsoninvalid character 'U' in string escape codeThe same path input
Official Go 1.27 scanner source
Java / JacksonUnrecognized character escape 'U' (code 85)The same path input
Source-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 input
Official .NET 10 resource template with U substituted; path and location omitted
C# / Json.NETBad JSON escape sequence: \U.The same path input
Json.NET 13.0.4 source template; path, line and position suffix omitted
PHP / json_last_error_msg()Syntax errorThe same path input
Official 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.

JAVASCRIPT
const text = String.raw`{"path":"C:\Users\ada"}`;
JSON.parse(text);
// Bad escaped character in JSON at position 12

A regular expression was copied into a value

The pattern \d+ must be written as \\d+ inside a JSON string.

PYTHON
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.

JAVASCRIPT
JSON.parse(String.raw`{"name":"O\'Brien"}`);
// Bad escaped character in JSON at position 11

How to fix it

  1. Find the backslash at the reported position. V8 and Firefox point at the character after it; Python points at the backslash itself.
  2. 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.
  3. Fix the producer: serialize the original string with JSON.stringify, json.dumps or your platform serializer instead of inserting raw text between quotes.
  4. 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.

JAVASCRIPT
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\ada

Serialize in Python and use raw strings for literals

Raw strings keep backslashes for the value; json.dumps escapes them for JSON.

PYTHON
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) == value

Find the exact position with Tool Kumar

  1. Copy the exact input text, including its original whitespace. For HTTP failures, copy the raw response body.
  2. Open the Tool Kumar JSON validator and paste the text. The document stays in your browser.
  3. 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.
  4. 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.

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.