Invalid escape sequence in JSON: How to Fix It in PHP
PHP json_decode() fails when a string has a backslash that JSON does not allow. See the common causes, such as Windows paths, regex and \' , and how to…
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →The error means a string contains a backslash followed by a character that JSON does not allow as an escape. Fix it by doubling the backslash or removing it.
What “Invalid escape sequence in JSON” means
The JSON standard allows only a short list of escapes after a backslash: \" \\ \/ \b \f \n \r \t and \uXXXX with four hex digits. Any other backslash pair is invalid. PHP's json_decode() does not print this exact text. It returns null, and json_last_error_msg() reports "Syntax error". The wording "Invalid escape sequence" comes from other parsers and tools that read the same bad input.
Causes and the fix for each
Windows path with single backslashes
\U and \d are not valid escapes, so the parser stops at the first one.
Before (fails):{"path": "C:\Users\data"}
After (valid):
{"path": "C:\\Users\\data"}
Regex pattern written with single backslashes
A pattern like \d+ puts \d inside a JSON string, and \d is not an allowed escape.
Before (fails):{"pattern": "\d+"}
After (valid):
{"pattern": "\\d+"}
Escaped single quote
JSON strings use double quotes, and \' is not a valid escape. This often comes from addslashes() or hand-built strings.
Before (fails):{"note": "it\'s done"}
After (valid):
{"note": "it's done"}
Broken \u escape
\u must be followed by exactly four hex digits. Anything shorter or non-hex is rejected.
Before (fails):{"c": "\u12G4"}
After (valid):
{"c": "\u00e9"}
C-style hex escape
JSON has no \x escape. Use the plain character or a \u escape.
Before (fails):{"c": "\x41"}
After (valid):
{"c": "A"}
How to fix it in PHP
- Call json_last_error_msg() right after json_decode() and log it. A bad escape shows up as "Syntax error" in PHP.
- Paste the raw string into the Validator to find the line and column of the first bad backslash.
- Fix the producer first. Build the data as a PHP array and call json_encode(). It doubles backslashes for you.
- If you edit the text by hand, double each literal backslash, change \' to ', and replace \x41 style escapes with plain characters or \u0041.
- If the input comes from a source you do not control, run it through the Repair tool or the helper below, then decode again.
- Do not run stripslashes() on the whole document. It removes the backslashes of valid escapes like \n and \" and can break good JSON.
<?php
function decodeJson(string $raw): array
{
$data = json_decode($raw, true);
if (json_last_error() === JSON_ERROR_NONE) {
return $data;
}
// Double any backslash that is not part of a valid escape.
$fixed = preg_replace('/\\\\[\"\\\\\/bfnrtu](*SKIP)(*FAIL)|\\\\/', '\\\\\\\\', $raw);
$data = json_decode($fixed, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('Bad JSON: ' . json_last_error_msg());
}
return $data;
}
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Syntax error (PHP json_last_error_msg) | Bad backslash escape, or any other syntax problem | Validate the text and double the stray backslash |
| Invalid escape sequence in JSON | Backslash followed by a character JSON does not allow | Use \\ for a literal backslash |
| Bad escaped character | Same problem reported by another parser | Remove or double the backslash |
| Invalid \uXXXX escape | \u not followed by four hex digits | Write all four hex digits |
| Control character error, possibly incorrectly encoded | Raw newline or tab inside a string, not a backslash escape | Replace it with \n or \t |
| Malformed UTF-8 characters, possibly incorrectly encoded | Input is not valid UTF-8 | Convert the input to UTF-8 before decoding |
Related errors
- Invalid escape sequence in JSON: How to Fix It in Python
- Invalid escape sequence in JSON: Fix JSON.parse() in JavaScript
- Unterminated string in JSON: How to Fix It in PHP
- Invalid control character in JSON: How to Fix It in Python
- Invalid JSON Meaning: What It Is and How to Fix It
Key takeaways
- A backslash in JSON must be followed by one of: " \ / b f n r t u.
- PHP reports this as a generic "Syntax error" from json_last_error_msg().
- Windows paths and regex patterns are the usual source.
- Build JSON with json_encode() instead of string concatenation.
- Check the failing text in the Validator, then repair it with the Repair tool if you cannot fix the source.
FAQ
Why does PHP say "Syntax error" and not "Invalid escape sequence"?
json_decode() uses one error code, JSON_ERROR_SYNTAX, for most malformed input, including bad escapes. The more specific wording comes from other parsers and tools.
Is \/ a valid escape in JSON?
Yes. The standard allows \/ as an escape for the forward slash. A plain / is also valid.
Does json_encode() produce invalid escapes?
No, json_encode() escapes backslashes and quotes as the standard requires. Bad escapes usually come from strings built by hand or from another system.
How do I write a literal backslash in JSON?
Write two backslashes. The parser reads \\ as one backslash character.
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 →The JSON Medic newsletter
One JSON error and its fix, weekly, plus new guides and tools. Free.
We send a confirmation link first. Unsubscribe with one click in any mail. See the privacy policy.
Related Articles