Ad Space — Top Banner

JSON decode error

PHP Programming Language

Severity: Moderate

What Does This Error Mean?

PHP's json_decode() function returns null when it cannot parse the JSON you gave it. Unlike most PHP errors, this does not show an error message on screen — it silently returns null. You have to check for the error yourself using json_last_error() or json_validate() in PHP 8.3. Common causes include malformed JSON, unexpected characters, or encoding issues.

Affected Models

  • PHP 5.2+
  • PHP 7.x
  • PHP 8.x
  • All PHP versions with json extension

Common Causes

  • The JSON string is malformed — missing quotes, a trailing comma, or incorrect brackets
  • The input is not JSON at all — it might be an HTML error page or plain text from an API failure
  • The JSON contains special characters or non-UTF-8 encoding that trips up the parser
  • The JSON is nested too deeply — PHP has a default recursion limit of 512 levels
  • A very large number in the JSON exceeds PHP's integer range and causes a parse failure

How to Fix It

  1. After calling json_decode(), check for errors: if (json_last_error() !== JSON_ERROR_NONE) { echo json_last_error_msg(); }

    json_last_error_msg() returns a human-readable description of what went wrong with the last JSON operation.

  2. Print the raw JSON string before decoding it: echo $jsonString; — make sure it looks like valid JSON.

    If the string starts with '<' it is an HTML page (probably an error page from the API). If it starts with '{' or '[' it is likely valid JSON.

  3. Paste the raw JSON into a validator like jsonlint.com to find syntax errors.

    The validator will highlight the exact location of any formatting problem in the JSON.

  4. Make sure the JSON is encoded in UTF-8. Use mb_convert_encoding($jsonString, 'UTF-8') if you suspect an encoding issue.

    PHP's JSON parser requires UTF-8. JSON with Latin-1 or Windows-1252 characters will fail to decode.

  5. In PHP 8.3+, use json_validate($jsonString) to check if a string is valid JSON before decoding it.

    For older PHP, use json_decode() and then immediately check json_last_error() === JSON_ERROR_NONE.

When to Call a Professional

JSON decode errors are usually solvable by a developer. If you are consuming a third-party API and the JSON keeps failing, the API may be returning malformed data. Contact the API provider's support team with the raw response you are receiving.

Frequently Asked Questions

Why does json_decode() return null without any error?

PHP's json_decode() returns null both when the input is the JSON string 'null' AND when parsing fails. You cannot tell the difference just by checking for null. Always call json_last_error() after decoding to check whether parsing succeeded.

What is the difference between JSON_ERROR_NONE and a null return?

JSON_ERROR_NONE means the decode was successful. A null return with a non-zero error code means the decode failed. A null return with JSON_ERROR_NONE means the JSON string was literally 'null', which is valid JSON. Always check the error code, not just the return value.

How do I decode JSON into a PHP array instead of an object?

Pass true as the second argument: $data = json_decode($json, true); By default, json_decode() returns a stdClass object. Passing true returns an associative array, which is often easier to work with in PHP.