Invalid escape in JSON: what “Bad escaped character” means and how to fix it
Updated
A backslash promises an escape. \U doesn’t keep the promise. Inside a JSON string a backslash may be followed by exactly nine things, and a Windows path or a regular expression usually brings something else.
A Windows path that breaks JSON
The single most common case: a path pasted into a string as it is. \U and \d are not JSON escapes, so the parser stops at the first one:
{"path":"C:\Users\dev"}Double every backslash, or use forward slashes, which Windows accepts in paths too. Both lines below are valid:
{"path":"C:\\Users\\dev"}
{"path":"C:/Users/dev"}Worse than the error is the path that doesn’t fail. "C:\temp\new" is valid JSON: \t is a tab and \n a line break, so it parses into nine characters with neither backslash in them. Regular expressions in configs hit the same wall: "\d+" is an error, "\\d+" is the pattern you meant.
Which escapes JSON allows (RFC 8259)
RFC 8259, section 7 lists every escape there is. After the backslash comes one of eight characters, or u and exactly four hex digits:
\" quotation mark \\ backslash \/ slash
\b backspace \f form feed \n line feed
\r carriage return \t tab \uXXXX any UTF-16 code unitThere is no \', no \x41, no \0, no \a or \v, and no long form like \U0001F600. A character outside the Basic Multilingual Plane is written as a UTF-16 surrogate pair: U+1F600 is \uD83D\uDE00. \u1F600 is not an error, just a surprise: \u takes four digits, so that is U+1F60 followed by 0.
Invalid escape messages by parser
Two inputs: the path above and {"s":"\u12G4"}, where G is not a hex digit. Every message comes from running the parser on 2026-11-08; Firefox’s from the SpiderMonkey source. Go 1.27 quotes the sequence in backquotes, so its two messages follow the table as printed.
| Parser | For C:\Users | For \u12G4 |
|---|---|---|
| Chrome 156, Node 24–25 (V8) | Bad escaped character in JSON at position 12 (line 1 column 13) | Bad Unicode escape in JSON at position 10 (line 1 column 11) |
| Firefox (SpiderMonkey) | bad escaped character | bad Unicode escape |
| Safari 27 (JavaScriptCore) | JSON Parse error: Invalid escape character U | JSON Parse error: "\u12G4" is not a valid unicode escape |
| Python 3.9–3.14 | Invalid \escape: line 1 column 12 (char 11) | Invalid \uXXXX escape: line 1 column 8 (char 7) |
| Go 1.25–1.26 | invalid character 'U' in string escape code | invalid character 'G' in \u hexadecimal character escape |
| Go 1.27 | Below the table | Below the table |
| Java, Jackson 2.15–2.23 | Unrecognized character escape 'U' (code 85) | Unexpected character ('G' (code 71)): expected a hex-digit for character escape sequence |
| C#, System.Text.Json 10 | 'U' is an invalid escapable character within a JSON string. The string should be correctly escaped. | 'G' is not a hex digit following '\u' within a JSON string. The string should be correctly escaped. |
| C#, Newtonsoft.Json 13 | Bad JSON escape sequence: \U. Path 'path', line 1, position 13. | Invalid Unicode escape sequence: \u12G4. Path 's', line 1, position 8. |
| PHP 8.3 | Syntax error | Syntax error |
| Rust, serde_json 1.0 | invalid escape at line 1 column 13 | invalid escape at line 1 column 12 |
| jq 1.7.1 | Invalid escape at line 1, column 22 | Invalid characters in \uXXXX escape at line 1, column 13 |
| MySQL 8.4: JSON column | Invalid JSON text: "Invalid escape character in string." at position 11 | Invalid JSON text: "Incorrect hex digit after \u escape in string." at position 6 |
| JSON Statham Validator | Invalid escape, line 1, column 13 | Invalid escape, line 1, column 11: “not a full \u escape” |
invalid escape sequence `\U` in string
invalid escape sequence `\u12G4` in stringPython points at the backslash, V8 and our validator at the character after it. Some parsers can be told to accept any escape, and that is worse than the error: Jackson’s JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER reads C:\Users as C:Users, silently dropping the backslash. Newtonsoft.Json accepts \' by default.
How to escape backslashes in JSON
Don’t double backslashes by hand: give the raw string to a serializer and it writes \\ for you. Every snippet below was run and prints {"path":"C:\\Users\\dev"} (Python adds a space after the colon).
JavaScript
const path = String.raw`C:\Users\dev`;
const json = JSON.stringify({ path });
console.log(json); // {"path":"C:\\Users\\dev"}
console.log(JSON.parse(json).path === path); // truePython: two layers of escaping
import json
path = r"C:\Users\dev"
print(json.dumps({"path": path})) # {"path": "C:\\Users\\dev"}
data = json.loads(r'{"path": "C:\\Users\\dev"}')
print(data["path"] == path) # TrueWithout the r prefix, Python turns \\ into \ before JSON sees the text, and json.loads stops with Invalid \escape. The same double processing happens in JavaScript string literals, which is why the JavaScript example uses String.raw.
Go
import "encoding/json"
func pathJSON() ([]byte, error) {
return json.Marshal(map[string]string{"path": `C:\Users\dev`})
}Java (Jackson)
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
static String pathJson(ObjectMapper mapper) throws JsonProcessingException {
return mapper.writeValueAsString(Map.of("path", "C:\\Users\\dev"));
}C# (System.Text.Json)
using System.Text.Json;
var json = JsonSerializer.Serialize(new { path = @"C:\Users\dev" });
Console.WriteLine(json); // {"path":"C:\\Users\\dev"}PHP
$json = json_encode(['path' => 'C:\Users\dev'], JSON_THROW_ON_ERROR);
echo $json; // {"path":"C:\\Users\\dev"}How to stop invalid escapes from coming back
- Serialize, don’t type. Every serializer escapes backslashes and quotes correctly; hand-written JSON in code is where
\Ucomes from. - Prefer forward slashes in paths you store.
C:/Users/devneeds no escaping at all and works in Windows APIs. - Double the backslashes in regular expressions. A JSON config holding a pattern needs
"\\d+","\\s","\\.". - Search for single-letter escapes in paths. A path that parses fine but contains a tab or a line break, like
C:\temp\new, has lost its backslashes.
Check escapes in the validator
Try it: open the invalid-escape sample in the Validator. The card names the sequence, \U for the path, and lists the nine allowed escapes. For a short or broken \u it points at the first character that isn’t a hex digit. A raw line break right after a backslash is reported as a control character instead.
- Control character in string: a raw tab or line break that should have been
\tor\n. - Unclosed string: a path that ends with a backslash,
"C:\temp\", escapes its own closing quote. - Single quotes:
\'doesn’t exist because JSON strings never use single quotes. - All JSON errors
JSON escape sequences FAQ
Which escape sequences are valid in JSON?
\", \\, \/, \b, \f, \n, \r, \t and \u followed by exactly four hex digits. Nothing else: no \', \x41, \0 or \U0001F600.
How do I write a Windows path in JSON?
Double every backslash, "C:\\Users\\dev", or use forward slashes, "C:/Users/dev". A serializer does the doubling for you.
How do I put an emoji into a JSON string with \u?
As a surrogate pair: U+1F600 is \uD83D\uDE00. Or write the character itself, since JSON is UTF-8. \u1F600 is read as U+1F60 followed by 0.
Is \/ really a valid escape?
Yes. Escaping the slash is allowed and changes nothing. PHP’s json_encode does it by default, so https:\/\/ in its output is correct.