How To Fix PHP Fatal Error: Call To Undefined Function Json_Decode()?

Published September 25, 2024

Problem: PHP Fatal Error with json_decode()

The PHP fatal error "Call to undefined function json_decode()" happens when using the json_decode() function in a PHP script. This error shows that the JSON extension is not enabled or installed in the PHP environment.

Diagnosing the Problem

Checking PHP version

To diagnose the "Call to undefined function json_decode()" error, check your PHP version. You can verify the PHP version in two ways:

  1. In the command line: Open a terminal or command prompt and type:

    php -v

    This command shows the installed PHP version.

  2. In the web environment: Create a PHP file with this code and run it through your web server:

    <?php
    echo phpversion();
    ?>

    This displays the PHP version used by your web server.

Tip: Check PHP version in .htaccess

If you're using Apache, you can also check the PHP version by adding this line to your .htaccess file:

php_info();

This will display detailed PHP configuration information, including the version.

Confirming JSON extension installation

After checking the PHP version, verify if the JSON extension is installed and enabled:

  1. Using phpinfo() to check for JSON module: Create a PHP file with this code and run it through your web server:

    <?php
    phpinfo();
    ?>

    Look for a section titled "json" in the output. If it's present, the JSON module is installed.

  2. Examining PHP configuration files:

    • Find your php.ini file. Its location varies depending on your system and PHP installation.
    • Open the php.ini file in a text editor.
    • Search for "json" or "extension=json.so" (on Unix-based systems) or "extension=php_json.dll" (on Windows).
    • If the line starts with a semicolon, remove it to enable the JSON extension.

By following these steps, you can determine if the JSON extension is properly installed and enabled in your PHP environment.

Solutions to Fix the Error

Installing the JSON extension

To fix the "Call to undefined function json_decode()" error, you may need to install the JSON extension. The steps are different for each operating system:

For Ubuntu or Debian-based systems:

  1. Open a terminal
  2. Run this command:
    sudo apt-get install php-json

For CentOS or RHEL-based systems:

  1. Open a terminal
  2. Run this command:
    sudo yum install php-json

For Windows systems:

  1. Find your PHP installation directory
  2. Locate the php.ini file
  3. Remove the semicolon at the start of the line extension=php_json.dll

Tip: Verify JSON Extension Installation

After installing the JSON extension, you can verify its installation by running the following PHP command in your terminal:

php -r "echo json_encode(['JSON extension' => 'installed']);"

If the JSON extension is correctly installed, you should see the output: {"JSON extension":"installed"}

Updating PHP to the latest version

Updating PHP to the latest version can fix many issues, including JSON-related problems. Benefits of updating PHP include:

  • Bug fixes and security updates
  • Better performance
  • New features and functions

To update PHP safely:

  1. Back up your PHP settings and applications
  2. Check if your code works with the new PHP version
  3. Update PHP using your system's package manager
  4. Test your applications after the update

Modifying PHP configuration

If the JSON extension is installed but not active, you may need to change your PHP settings:

  1. Find your php.ini file
  2. Open it in a text editor
  3. Look for the line about the JSON extension (e.g., extension=json.so or extension=php_json.dll)
  4. If the line starts with a semicolon, remove it
  5. Save the file and close the text editor

After making these changes, restart your web server to use the new settings.

Verifying the Fix

Testing json_decode() functionality

To check if the json_decode() function works after applying the fixes, use this PHP script:

<?php
$json_string = '{"name": "John", "age": 30}';
$decoded_data = json_decode($json_string, true);

if ($decoded_data === null && json_last_error() !== JSON_ERROR_NONE) {
    echo "JSON decoding failed: " . json_last_error_msg();
} else {
    echo "JSON decoding successful. Decoded data: ";
    print_r($decoded_data);
}
?>

Run this script through your web server or command line. If it shows the decoded data without errors, the json_decode() function works.

Tip: Testing with Invalid JSON

To test error handling, try decoding an invalid JSON string:

$invalid_json = '{"name": "John", "age": 30,}'; // Extra comma
$result = json_decode($invalid_json);
echo json_last_error_msg(); // Should output "Syntax error"

Restarting web server

After changing PHP settings or installing extensions, restart your web server. This applies the new settings. Here are commands for restarting web servers:

For Apache on Linux:

sudo service apache2 restart

For Nginx on Linux:

sudo service nginx restart

For Apache on Windows:

  1. Open Command Prompt as Administrator
  2. Go to the Apache bin directory
  3. Run:
    httpd -k restart

For IIS on Windows:

  1. Open Command Prompt as Administrator
  2. Run:
    iisreset

After restarting the web server, test your PHP application to confirm that the json_decode() function works as expected.