In-depth Analysis and Solution for PHP 'Call to undefined function json_decode()' Error

Nov 27, 2025 · Programming · 9 views · 7.8

Keywords: PHP | JSON | Ubuntu | Apache | Troubleshooting

Abstract: This article provides a comprehensive analysis of the 'Call to undefined function json_decode()' error in PHP environments, focusing on the licensing issues with PHP JSON extensions in Debian/Ubuntu systems. It offers complete troubleshooting procedures, specific steps for installing JSON extensions, and detailed technical background on licensing controversies to help developers resolve this common issue effectively.

Problem Phenomenon and Initial Analysis

When the PHP Fatal error: Call to undefined function json_decode() error appears in Apache logs, many developers initially suspect PHP version issues. However, as reported by users, even when php --version shows PHP version 5.5.1 (higher than 5.1), the error persists. phpinfo() output also confirms the PHP version information and shows the JSON module listed, but function calls still fail.

Root Cause: Licensing Conflict

The core of this issue lies in the Debian distribution removing the original JSON extension starting from PHP 5.5rc2. The removal was due to the "The Software shall be used for Good, not Evil" clause in the JSON license conflicting with the Free Software Foundation's (FSF) definition of free software. FSF explicitly states that free software must allow users to run the program for any purpose (freedom 0), and this restrictive clause in the JSON license makes it considered non-free software.

It's important to emphasize that PHP official never removed JSON support. As Rasmus Lerdorf clearly stated in the PHP bug report: "We have not removed json and we will never release a version of php without json support built in. Any changes in 5.5 is due to whatever distro packaging you are using which we have no control over."

Solution: Installing Alternative Extension

For Ubuntu/Debian system users, the solution is to install the specifically provided JSON extension package:

sudo apt-get install php5-json

After installation, Apache service must be restarted for changes to take effect:

sudo service apache2 restart

If using PHP-FPM, the corresponding PHP-FPM service needs to be restarted:

sudo service php5-fpm restart

Verification and Troubleshooting

After installation, JSON extension functionality can be verified through:

php -r "echo json_encode(['test' => 'value']);"

If output shows {"test":"value"}, it indicates JSON functions are available. Additionally, JSON support status can be checked via command line:

php -i | grep json

Normal output should show JSON support enabled with relevant version information.

Technical Background and Alternative Implementation

The Debian community provides functionally equivalent alternative JSON extensions (such as remicollet's pecl-json-c) that avoid licensing issues while maintaining full compatibility with native JSON extensions. This approach represents the open-source community's balanced strategy of adhering to software freedom principles while ensuring functional availability.

Cross-Platform Solutions

While this article primarily addresses Debian/Ubuntu systems, similar issues may occur in other Linux distributions. For example, in CentOS 8, JSON support can be installed via:

sudo dnf install php-json

For PHP compiled from source, ensure the --disable-json option was not used during compilation.

Summary and Best Practices

The PHP JSON function undefined error is typically not a PHP version issue but rather a result of specific Linux distribution packaging strategies. Developers should understand their distribution's package management policies and prioritize checking the installation status of relevant extensions when encountering similar issues. Keeping systems updated and following distribution best practices can effectively prevent such compatibility problems.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.