PHP json_last_error() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
JSON

Definition

In PHP, json_last_error() returns the error code from the last json_decode() or json_encode() call. A result of 0 means there was no error.

PHP does not stop your script when JSON fails. It quietly returns null or false and remembers an error code. json_last_error() lets you read that code and compare it with constants such as JSON_ERROR_NONE and JSON_ERROR_SYNTAX.

Syntax

Call json_last_error() with the values you need. Start with this line.

PHP
if (json_last_error() !== JSON_ERROR_NONE) { /* handle error */ }
CallWhat it does
json_last_error()Returns the integer error code from the most recent JSON call.
json_last_error() === JSON_ERROR_NONEChecks that the last JSON call finished without problems.
json_last_error() === JSON_ERROR_SYNTAXChecks whether the last JSON call failed because of bad syntax.

json_last_error() takes no arguments. Call it right after json_decode() or json_encode(), because the next JSON call resets the value. Calls that use JSON_THROW_ON_ERROR do not change the global error state.

Library declarations
PHP
json_last_error(): int

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
intAn error code. 0 is JSON_ERROR_NONE. Other values include 1 (JSON_ERROR_DEPTH), 3 (JSON_ERROR_CTRL_CHAR), 4 (JSON_ERROR_SYNTAX), 5 (JSON_ERROR_UTF8), 7 (JSON_ERROR_INF_OR_NAN) and 8 (JSON_ERROR_UNSUPPORTED_TYPE).

PHP Examples

Example 1 — Check for No Error or Syntax Error

Decode a good and a bad JSON string, and read the error code after each one.

PHP
<?php
json_decode('{"ok":true}');
echo json_last_error(), PHP_EOL;
echo json_last_error() === JSON_ERROR_NONE ? 'No error' : 'Error', PHP_EOL;

json_decode('{oops}');
echo json_last_error(), PHP_EOL;
echo json_last_error() === JSON_ERROR_SYNTAX ? 'Syntax error found' : 'Other error', PHP_EOL;

How It Works

  1. The first JSON string is valid, so json_last_error() returns 0.
  2. 0 is the value of JSON_ERROR_NONE, so the first message says No error.
  3. The second JSON string has no quotes around the key, so decoding fails.
  4. The code 4 matches JSON_ERROR_SYNTAX.

Example 2 — The Error Resets and Ignores the Throw Flag

Each normal JSON call resets the error code. Calls with JSON_THROW_ON_ERROR leave the stored code alone.

PHP
<?php
json_decode('{bad');
echo "After bad: ", json_last_error(), PHP_EOL;

json_decode('[1,2]');
echo "After good: ", json_last_error(), PHP_EOL;

try {
    json_decode('{bad', false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
    echo "Caught: ", $e->getMessage(), PHP_EOL;
}
echo "After throw flag: ", json_last_error(), PHP_EOL;

How It Works

  1. The first call fails, so json_last_error() returns 4.
  2. The next valid call resets the stored error back to 0.
  3. The third call fails but uses JSON_THROW_ON_ERROR, so it throws an exception.
  4. The stored error code is not changed by that call, so it is still 0.

Example 3 — Turn Error Codes Into Friendly Messages

Use a match expression to turn error codes into short messages for your users.

PHP
<?php
function describe(int $code): string
{
    return match ($code) {
        JSON_ERROR_NONE => 'No error',
        JSON_ERROR_DEPTH => 'Too deep',
        JSON_ERROR_SYNTAX => 'Bad syntax',
        JSON_ERROR_UTF8 => 'Bad UTF-8',
        default => 'Other error',
    };
}

json_decode('[1]');
echo describe(json_last_error()), PHP_EOL;

json_decode('[[1]]', true, 1);
echo describe(json_last_error()), PHP_EOL;

json_decode("{'a':1}");
echo describe(json_last_error()), PHP_EOL;

json_encode(hex2bin('b131'));
echo describe(json_last_error()), PHP_EOL;

How It Works

  1. describe() receives an error code and returns a short text using match.
  2. A valid decode gives JSON_ERROR_NONE, and a depth of 1 on nested arrays gives JSON_ERROR_DEPTH.
  3. JSON does not allow single quotes around keys, so that decode gives JSON_ERROR_SYNTAX.
  4. Encoding invalid UTF-8 bytes gives JSON_ERROR_UTF8, which shows json_last_error() also works after json_encode().

Frequently Asked Questions

It returns an integer error code for the last json_decode() or json_encode() call. 0 means no error, and other numbers match constants like JSON_ERROR_SYNTAX or JSON_ERROR_DEPTH.
json_last_error() gives a number that is easy to compare in code. json_last_error_msg() gives a readable sentence that is easy to show in logs or messages.
No. When you pass JSON_THROW_ON_ERROR, PHP throws a JsonException and leaves the global error state unchanged, so catch the exception instead of checking json_last_error().
Call it right after json_decode() or json_encode(). Any later JSON call overwrites the value, so waiting too long means you lose the original error.
Because null is also a valid JSON value. The text 'null' decodes to null with no error, so only the error code can tell a real failure from a valid null.

Next: json_last_error_msg()

json_last_error_msg() returns a readable text message for the last json_decode() or json_e.

json_last_error_msg() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful