Why it happens
Inside a JSON string, a backslash must start one of nine escapes. In the sample, \l in D:\logs\api is not one of them, so parsing stops at the l. Most common sources first:
1. Windows paths. D:\logs\api breaks on \l. The paths that parse are worse: "C:\new\tables" contains \n and \t, so it loads without an error as C:, a line break, ew, a tab and ables.
2. Regular expressions. {"zip": "^\d{5}$"} fails on \d. Every backslash in a pattern needs doubling in JSON.
3. An escaped apostrophe. "Don\'t retry" is fine in a JavaScript or PHP string and invalid in JSON. Hand-escaped text and PHP’s addslashes() produce it.
4. Escapes from other languages. \x41, \0 and \a come from C, Python and shell strings. JSON writes the first one as \u0041.
How to fix bad escaped character in JSON
Double the backslash, or use forward slashes
{
"logDir": "D:\\logs\\api",
"level": "info"
}
Node.js, Python and most Windows programs also accept D:/logs/api.
Let a serializer write the string
import json
print(json.dumps({"logDir": r"D:\logs\api"})) # {"logDir": "D:\\logs\\api"}
JSON.stringify does the same in JavaScript.
Store regex patterns with doubled backslashes
{"zip": "^\\d{5}$"}
const zipPattern = new RegExp(config.zip); // /^\d{5}$/
zipPattern.test('02139'); // true
PHP: json_encode instead of addslashes
echo json_encode(['msg' => "Don't retry"]); // {"msg":"Don't retry"}
Repair and the escape tool
The JSON validator names the escape, for example Invalid escape \l in a string. JSON Repair deletes the backslash of an invalid escape. That is right for \' and wrong for paths and patterns: it turns D:\logs\api into D:logsapi. For those, double the backslashes yourself, or paste the raw value into the JSON escape tool, which returns "D:\\logs\\api".
Same error in other languages
- Python:
json.decoder.JSONDecodeError: Invalid \escape: line 2 column 16 (char 17). Python points at the backslash, one character before Chrome. - Go:
invalid character 'l' in string escape code - PHP:
json_last_error_msg()returnsSyntax error. - VS Code:
Invalid escape character in string.(261) - A raw line break or tab inside a string is a different error: Bad control character in string literal.
For other parse errors, see common JSON errors.