PHP Fatal error: Call to undefined function json_decode()

198,870

Solution 1

Using Ubuntu?

Short answer:

sudo apt-get install php7.2-json

(or php7.1-json or php5-json depending on the PHP version you're running)

Then of course make sure you restart Apache:

sudo service apache2 restart

Or if you are using PHP-FPM:

sudo service php7.2-fpm restart

(Or php7.1-fpm or php5-fpm)

Explanation

Debian has removed the previous JSON extension as of PHP 5.5rc2 due to a license conflict.

The JSON license has a clause which states:

The Software shall be used for Good, not Evil.

This causes a problem with Free Software Foundation's definition of free software which states:

The freedom to run the program, for any purpose (freedom 0).

FSF goes on to specifically list the JSON license as nonfree.

Yes it seems a bit silly. Nevertheless Debian has removed the non-compliant JSON extension, and instead offered a replacement extension that is functionally equivalent.

To be clear: PHP itself has NOT removed JSON, it's still in master. This is a distro / package manager issue.

Rasmus makes it pretty clear:

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.

More details

http://iteration99.com/2013/php-json-licensing-and-php-5-5/

http://liorkaplan.wordpress.com/2013/06/01/bye-bye-non-free-php-json-extension/

https://bugs.php.net/bug.php?id=63520

http://philsturgeon.co.uk/blog/2013/08/fud-cracker-php-55-never-lost-json-support

Solution 2

With Ubuntu :

sudo apt-get install php5-json
sudo service php5-fpm restart

Solution 3

Solution for LAMP users:

apt-get install php5-json
service apache2 restart

Source

Solution 4

As a RHEL 8 user, this was fixed with:

yum install php-json

And then reloading nginx and php-fpm. So basically the json libraries where missing from the default php install.

This should also work on CENTOS 8.

Solution 5

If you're using phpbrew try to install json extension to fix error with undefined function json_decode():

phpbrew ext install json
Share:
198,870

Related videos on Youtube

MirroredFate
Author by

MirroredFate

Occasionally I write code. Sometimes it runs.

Updated on July 14, 2022

Comments

  • MirroredFate
    MirroredFate almost 2 years

    Apache is logging PHP Fatal error: Call to undefined function json_decode(). After some googling, it seems this problem is a result of not having the latest version of php. Oddly, running php --version ouputs

    PHP 5.5.1-2+debphp.org~precise+2 (cli) (built: Aug  6 2013 10:49:43) 
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
        with Zend OPcache v7.0.2-dev, Copyright (c) 1999-2013, by Zend Technologies
        with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
    

    Clearly showing that my php version is greater than 5.1.

    Any ideas as to what could cause this, or the steps to take to find out?

    EDIT: The result of a script echoing phpversion is 5.5.1-2+debphp.org~precise

    EDIT: The result of phpinfo() is also PHP Version 5.5.1-2+debphp.org~precise+2

    Furthermore, in the phpinfo(), it lists the module JSON and the module authors (Omar Kilani, Scott MacVicar).

    • Orangepill
      Orangepill over 10 years
      Check the disable_functions ini directive to see if json_decode is in that list
    • tcmitche
      tcmitche over 10 years
      Does the json module show up in your phpinfo() output?
    • MirroredFate
      MirroredFate over 10 years
      disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexi‌​ted,pcntl_wifstopped‌​,pcntl_wifsignaled,p‌​cntl_wexitstatus,pcn‌​tl_wtermsig,pcntl_ws‌​topsig,pcntl_signal,‌​pcntl_signal_dispatc‌​h,pcntl_ge\ t_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaiti‌​nfo,pcntl_sigtimedwa‌​it,pcntl_exec,pcntl_‌​getpriority,pcntl_se‌​tpriority,
    • jgb
      jgb over 10 years
      Ubuntu 12.04 has a package: php-services-json. Maybe this helps...
    • mario
      mario over 10 years
      @jgb That's the PEAR package Services_Json. Which is also quite encompassing, parses a bit Javascript, not just JSON even. But there are other userland implementations directly reimplementing json_decode.
  • MirroredFate
    MirroredFate over 10 years
    I came back to mention that it appears as if 5.5.0 is fine with JSON, it is only 5.5.1 that breaks.
  • Luis Lobo Borobia
    Luis Lobo Borobia over 10 years
    5.5.3 does not have it neither (Ubuntu 13.10)
  • Pavel
    Pavel over 10 years
    Don't forget to restart apache after installing json module with the command Chris mentioned.
  • rickfoosusa
    rickfoosusa about 10 years
    If php-json were a dependency of php-common this would all go away...bugs.launchpad.net/ubuntu/+source/ubiquity/+bug/12800‌​30 ...please vote if this was a problem for you.
  • Jürgen A. Erhard
    Jürgen A. Erhard about 10 years
    Ever wonder who defines this "Evil" here? And you'd use software that may or may not allow the use you put it to? Me? I wouldn't.
  • recursion.ninja
    recursion.ninja almost 10 years
    Does anyone find it ironically "evil" that this software licensing exclusion cost me an hour of research time?
  • xilef
    xilef over 9 years
    If a restart fails to solve the error, kill the apache2 processes and start: sudo pkill apache2; service apache2 start
  • E Ciotti
    E Ciotti over 9 years
    still didn't work, I had to remove and reinstall php5-fpm + extensions (ubuntu 14, PHP 5.5.9-1ubuntu4.5)
  • JRam13
    JRam13 about 9 years
    I just spent a HOURS debugging my php code! Are you kidding me with this... FML
  • gies0r
    gies0r over 7 years
    Same in FreeBSD: Use pkg install php56-json
  • emix
    emix over 5 years
    Guzzle's json_decode is using php's json_decode with additional error check, so your answer is invalid
  • Smit Patel
    Smit Patel almost 5 years
    What about apt-get install php7.2-json for 7.2. I tried this but still not working. Giving me error on laravel app php RuntimeException PHP's json extension is required to use Monolog's NormalizerFormatter