PHP json_decode() returns null
If json_decode() can’t parse the JSON you give it, it returns null without showing an error or a warning, so the problem is easy to miss. To see what went wrong, call json_last_error_msg() right after it. You’ll get a short message, such as Syntax error or Malformed UTF-8 characters, and the table below explains each one.
Quick fix
- Print
json_last_error_msg()right afterjson_decode(). - Find that message in the table below to see what it means.
- If it says
Syntax error, paste the JSON below to find and fix the mistake.
Fix your JSON
Getting the real error
There are two ways to find out what went wrong. The first works on any PHP version; the second, available since PHP 7.3, makes json_decode() throw an exception instead:
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo json_last_error_msg(); // e.g. "Syntax error"
}
// PHP 7.3+: throw instead of returning null
try {
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
echo $e->getMessage();
}If you can, use JSON_THROW_ON_ERROR. Besides giving you the message, it means a failed decode can never be mistaken for JSON that really is null.
What each message means
Syntax error is the one you’ll see most, and unfortunately it doesn’t say where the problem is. Pasting the JSON into the fixer above, or the validator, will point to the exact line.
Syntax error | Invalid JSON: single quotes, a trailing comma, a missing comma, text after the JSON, an empty string, or a BOM at the start. Fix |
Malformed UTF-8 characters, possibly incorrectly encoded | The text isn’t UTF-8, often Windows-1252 or Latin-1 from a database or older file. |
Control character error, possibly incorrectly encoded | A raw line break or tab inside a string. Fix |
Maximum stack depth exceeded | The JSON is nested deeper than the depth argument (512 by default). |
No error | The JSON is valid and is literally null. |
Syntax error on JSON that looks fine
If the JSON looks fine and even passes a validator, but PHP still says Syntax error, the file probably starts with an invisible byte order mark. Some editors on Windows add one when saving as UTF-8. Strip it before decoding:
$json = preg_replace('/^\xEF\xBB\xBF/', '', $json);Malformed UTF-8 characters
json_decode() only accepts UTF-8. This error means the text is in another encoding, often Windows-1252 or Latin-1 from an older database or file. You’ll usually notice it on accented characters. Convert it to UTF-8 first. Or, if losing the odd character is acceptable, have PHP replace the bad bytes:
// Convert from the real encoding
$json = mb_convert_encoding($json, 'UTF-8', 'Windows-1252');
// Or replace invalid bytes with U+FFFD (PHP 7.2+)
$data = json_decode($json, true, 512, JSON_INVALID_UTF8_SUBSTITUTE);If the text comes from MySQL, it’s better to fix it at the source: set the connection charset to utf8mb4, and the data will arrive as UTF-8 in the first place.
The same error elsewhere
| JavaScript | Expected property name or '}' in JSON at position 4 |
|---|---|
| Python | JSONDecodeError: Expecting property name enclosed in double quotes |
| Go | invalid character '\'' looking for beginning of object key string |