Enabling json_encode And json_decode in PHP < 5.2.0 (JSON)

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, October 29, 2010

Follow me on Twitter as @mattiasgeniar

JSON support is included in PHP version 5.2.0 and later, but if you’re running a default 5.1.x stack (like most CentOS systems, since it’s in the default repositories), you can get JSON support for your PHP 5.1 like this.

1. Via yum (CentOS 5.x)

[root@srv src] yum install php-pecl-json

2. Via PECL (CentOS 4.x)

Update your PHP.NET channel.

[root@srv src]# pecl channel-update pecl.php.net
Retrieving channel.xml from remote server
Channel pecl.php.net channel.xml is up to date

Search for the package.

[root@srv src]# pecl search json
Retrieving data...0%....50%....Matched packages, channel pecl.php.net:
=======================================
Package Stable/(Latest)      Local
json    1.2.1/(1.2.1 stable)       JavaScript Object Notation.
Note: This extension is now part of PHP Core, so use it instead. As of PHP 5.2.0.

And install it.

[root@srv src]# pecl install json
downloading json-1.2.1.tgz ...
Starting to download json-1.2.1.tgz (17,780 bytes)
......done: 17,780 bytes
11 source files, building
...

Have fun!



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.