json_encode() returns false

json_encode() returns false

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

json_encode() returns false

When json_encode() returns false, the immediate instinct is often to suspect a corrupted data structure or an environment limitation. In your specific case, you are using PHP 5.5 on Apache 2.4 and attempting to serialize a deeply nested array resulting from unserialize(). The fact that var_dump($array) works perfectly while $json fails suggests the issue lies within how PHP handles the conversion logic for that specific data shape, rather than the memory layout of the variable itself.

The most probable culprit is the interaction between the JSON_PRETTY_PRINT flag and the depth limit parameter in PHP 5.5. Unlike newer versions where arguments are strictly positional or clearly keyword-based, PHP 5.5 interprets the second argument as a string option (like JSON_PRETTY_PRINT) and the third as an integer depth. However, there is a known quirk in PHP 5.5 regarding how it processes these flags when combined with deep nesting.

If your array exceeds the internal recursion limit or if the specific combination of options triggers a boundary check inside the C extension responsible for JSON encoding, json_encode will abort and return false. The error message is not thrown; instead, you simply get a boolean false, which makes debugging difficult without checking the context.

To resolve this, you should first verify if the data structure itself is causing an infinite recursion or if the depth limit of 3 is actually hitting a hard constraint in your specific PHP build. While JSON_PRETTY_PRINT adds newlines and indentation, it increases the output size significantly. In older PHP versions like 5.5, generating large formatted strings can sometimes trigger internal buffer overflows or timeout mechanisms depending on how the Apache module (mod_php) is configured.

Here is a more robust approach that separates the concerns of formatting and depth control:

$flags = JSON_PRETTY_PRINT;
$options = [
    'depth' => 3,
];

// Check if the depth parameter is supported by the current PHP build
if (version_compare(PHP_VERSION, '5.5', '>=')) {
    // In PHP 5.5+, the depth parameter is supported but can be finicky
    // It's often safer to encode with flags first, then truncate manually if needed
    $json = json_encode($array, $flags);
    
    if ($json === false) {
        // Fallback: Try without depth limit to see if it's a buffer issue
        $json = json_encode($array, $flags); 
        if ($json !== false) {
            // Manually truncate output if necessary, though this is complex for pretty print
            // A better path in 5.5 might be disabling pretty print and handling formatting externally
            $json = json_encode($array); 
            $json = substr($json, 0, 300) . '...'; // Example truncation
        }
    }
}

However, a more direct fix often found in production environments running PHP 5.5 is to avoid passing the depth parameter alongside JSON_PRETTY_PRINT. The internal implementation of json_encode in older versions sometimes struggles to reconcile the memory overhead of formatting with the recursion check for depth limits. Try encoding without the depth limit first, then truncate the string if performance becomes an issue:

$json = json_encode($array, JSON_PRETTY_PRINT);

If you must strictly limit the output to three levels deep, consider processing the data in a post-encoding step or switching to JSON_UNESCAPED_UNICODE and JSON_UNESCAPED_SLASHES flags if they are relevant to your payload. These flags can sometimes reduce the complexity of the string generation process.

Another potential pitfall is the presence of circular references, though unserialize() usually handles these by converting them into arrays with integer keys for recursive children (as seen in your dump). If the children key points back to a parent that hasn't been fully resolved, or if there are hidden objects mixed into the array, json_encode will fail. Always ensure that every element in the array is strictly serializable: strings, integers, floats, booleans, null, arrays, and objects without cyclic references.

In modern PHP development, you might consider upgrading from PHP 5.5 if possible, as many edge cases regarding JSON encoding have been stabilized in PHP 7 and 8. If you are constrained to PHP 5.5, be wary of the max_depth parameter; it is often safer to omit it when using JSON_PRETTY_PRINT and handle depth limiting via string manipulation or a custom recursive walker instead. This prevents json_encode from attempting complex internal recursion checks that might trigger false negatives in older builds.

Our network