json_decode null 🐘 PHP

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

  1. Use json_decode($s, true, 512, JSON_THROW_ON_ERROR)
  2. Log the raw string when decoding fails
  3. Read raw bodies with file_get_contents('php://input')

Detailed fix by platform

PHP

  1. 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

  1. Raw — What does the string look like?
  2. 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.