json_decode() returns null / JSON_ERROR_SYNTAX: Syntax error
The string passed to json_decode isn’t valid JSON, so PHP returns null.
json_decode returns nullJsonException: Syntax errorMalformed UTF-8 characters Meaning
json_decode fails silently unless you check json_last_error() or use JSON_THROW_ON_ERROR. Common inputs that break: BOM at the start, HTML error pages, single quotes, trailing commas, or a body you never read.
Common causes
- Response is HTML or empty, not JSON
- UTF-8 BOM / invalid UTF-8
- Single quotes or trailing commas
- Reading $_POST instead of php://input
⚡ Quick fix
- Use
json_decode($s, true, 512, JSON_THROW_ON_ERROR) - Log the raw string when decoding fails
- Read raw bodies with
file_get_contents('php://input')
Detailed fix by platform
PHP
try { $data = json_decode($raw, true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $e) { error_log($e->getMessage() . ': ' . substr($raw, 0, 200)); }
How to diagnose
- Raw — What does the string look like?
- Error — json_last_error_msg()
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in PHP
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 13 Oct 2026