json_decode() returns null / JSON_ERROR_SYNTAX: Syntax error
The string passed to json_decode isn’t valid JSON, so PHP returns null.
Seen on:
PHP
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 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.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026