Skip to content
JSON Tidy
JSON errors

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

  1. Print json_last_error_msg() right after json_decode().
  2. Find that message in the table below to see what it means.
  3. If it says Syntax error, paste the JSON below to find and fix the mistake.

Fix your JSON

Runs in your browser. Nothing is uploaded.

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 errorInvalid 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 encodedThe text isn’t UTF-8, often Windows-1252 or Latin-1 from a database or older file.
Control character error, possibly incorrectly encodedA raw line break or tab inside a string. Fix
Maximum stack depth exceededThe JSON is nested deeper than the depth argument (512 by default).
No errorThe 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

JavaScriptExpected property name or '}' in JSON at position 4
PythonJSONDecodeError: Expecting property name enclosed in double quotes
Goinvalid character '\'' looking for beginning of object key string