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.