IBM: Convert XML to JSON in PHP

IBM’s developerWorks has posted an interesting article on converting XML to JSON using PHP. At first glance, the question would be “Why”? After all, both XML and JSON serve similar purposes and they both do their job. Almost. It turns out that people find it much easier to process JSON data in the browser than XML. Since many web services return XML, there needs to be a conversion process. Here’s where IBM’s article comes in. Here’s a quote where they talk a little about what they are trying to do.

More and more applications need to convert XML data into JSON. Several Web-based services are already popping up to do such conversions. The IBM T.J. Watson Research Center has developed a particular approach that uses PHP to do the conversion. This approach accepts XML string data as input and converts that into JSON-formatted data output. This PHP-based solution provides several benefits:

  • It can be run in a standalone mode, executed from the command line.
  • It can be included in an existing server-side code artifact.
  • It can be easily hosted as a Web service on the Web.

If you are working with web services, especially if you are proxying other’s services for your own use, you’ll want to read this article. It’s not a long read. If you are already familiar with XML and JSON then it’s even shorter because you can skip the “basics” sections. Most of the rest of the article is source code and explanations.


About Cal Evans

Many moons ago, at the tender age of 14, Cal touched his first computer. (We're using the term "computer" loosely here, it was a TRS-80 Model 1) Since then his life has never been the same. He graduated from TRS-80s to Commodores and eventually to IBM PCs. For the past 10 years, Cal has worked with PHP and MySQL on Linux OSX, and when necessary, Windows. He has built on a variety of projects ranging in size from simple web pages to multi-million dollar web applications. When not banging his head on his monitor, attempting a blood sacrifice to get a particular piece of code working, he enjoys building and managing development teams using his widely imitated but never patented management style of "management by wandering around". Cal is happily married to wife 1.32, the lovely and talented Kathy. Together they have 2 kids who were both bright enough not to pursue a career in IT. Cal blogs at and is the founder and host of Nomad PHP