PHP json_last_error_msg() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
JSON

Definition

In PHP, json_last_error_msg() returns a readable text message for the last json_decode() or json_encode() error. If there was no error, it returns the string No error.

This function is the friendly cousin of json_last_error(). Instead of a number, it gives you a sentence such as Syntax error, which is perfect for logs, debug output and error messages.

Syntax

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

PHP
echo json_last_error_msg();
CallWhat it does
json_last_error_msg()Returns the message for the most recent JSON call.
json_decode('{oops}'); json_last_error_msg()Returns Syntax error after a failed decode.
json_encode(NAN); json_last_error_msg()Returns Inf and NaN cannot be JSON encoded after a failed encode.

json_last_error_msg() takes no arguments and always returns a string. Read the message right after the JSON call, because the next call resets it. It does not report errors from calls that use JSON_THROW_ON_ERROR.

Library declarations
PHP
json_last_error_msg(): string

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
stringA message such as No error, Syntax error, Maximum stack depth exceeded, Malformed UTF-8 characters, possibly incorrectly encoded, or Type is not supported.

PHP Examples

Example 1 — Read the Error Message

Decode valid and invalid JSON, and print the message after each call.

PHP
<?php
json_decode('{"a":1}');
echo json_last_error_msg(), PHP_EOL;

json_decode('{oops}');
echo json_last_error_msg(), PHP_EOL;

How It Works

  1. The first JSON string is valid, so the message is No error.
  2. The second JSON string is missing quotes around the key.
  3. PHP stores a syntax error for that failed call.
  4. json_last_error_msg() turns the stored error into a readable sentence.

Example 2 — Messages From json_encode()

The function also describes encoding problems, such as values that JSON cannot represent.

PHP
<?php
json_encode(NAN);
echo json_last_error_msg(), PHP_EOL;

$file = fopen('php://memory', 'r');
json_encode($file);
echo json_last_error_msg(), PHP_EOL;
fclose($file);

How It Works

  1. JSON has no way to write NAN, so json_encode() fails.
  2. json_last_error_msg() reports that Inf and NaN cannot be encoded.
  3. A file handle is a resource, and resources cannot be turned into JSON.
  4. The message for that failure is Type is not supported.

Example 3 — A Safe Decode Helper

Wrap json_decode() in a helper that prints a friendly message when something goes wrong.

PHP
<?php
function safeDecode(string $json): ?array
{
    $data = json_decode($json, true);

    if (json_last_error() !== JSON_ERROR_NONE) {
        echo "Decode failed: ", json_last_error_msg(), PHP_EOL;
        return null;
    }

    return $data;
}

$result = safeDecode('{"a":1,"b":2}');
echo "Items: ", count($result), PHP_EOL;

safeDecode('{"a":');

var_dump(safeDecode('null'));

How It Works

  1. safeDecode() decodes the string and then checks json_last_error().
  2. Valid JSON returns an array, so count() prints 2.
  3. The broken JSON triggers the error branch, which prints the message from json_last_error_msg().
  4. The text null is valid JSON, so there is no message, and the helper just returns null.

Frequently Asked Questions

It returns a text description of the last JSON error, like Syntax error or Maximum stack depth exceeded. When there is no error it returns No error.
Use json_last_error() when your code needs to make a decision, and json_last_error_msg() when you want to show or log a readable message. Many scripts use both together.
No. With that flag PHP throws a JsonException, and the exception already has the message through getMessage(). The global error state is not updated.
You will often see No error, Syntax error, Maximum stack depth exceeded, Malformed UTF-8 characters, possibly incorrectly encoded, Inf and NaN cannot be JSON encoded, and Type is not supported.
It is better to log it and show a simple friendly message to visitors. The technical text can confuse users and may reveal details about how your code handles data.

Next: json_validate()

json_validate() checks whether a string contains valid JSON. It returns true or false and .

json_validate() 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