How to transform CCB XML to JSON

Here recently, I’ve seen a few questions in CCB’s Villiage pertaining to individuals trying to figure out how to change or transform their XML response to a JSON response.  Well, today’s your lucky day because we’re doing just that!

Should you Google this topic, there are many good and informative tutorials covering JSON in general.  I’ll make and take the time to share in this tutorial today with those of you that are technically inclined or those of you just coming up to speed in all this fast moving techy-programmy jargon in regards to the CCB API.

Today’s tutorial will build on and extend the search for CCB individual codebase, so let’s get started!

Why JSON over XML?

I’m not going to go deep into the weeds of ALL the various answers, but I will attempt to give you a simple and easy to understand high-level overview. So, here we go…

Until a few years ago, XML (eXtensible Markup Langauge) was the only viable option for those in need of open data interchange for the purposes of sharing data.  In short, JSON has trumped XML due to JSON being lightweight and more easy to parse than XML.

Here’s a question… have you not noticed these tutorials gobbling up memory, especially when attempting large sums of XML data, as you attempt to parse the XML via DOM manipulation?  This is one major reason to choose JSON over XML.

Another reason to choose JSON over XML is intuitiveness of the data model structure actually matching the data.  One way to view this is to read or dump identical data using XML and JSON, and then compare the two.  Which is more easy to read?  No worries, I’ll wait… go ahead and do it.

Now that you’re back, what do you think.  Often times, there’s a crowd of techies who think and believe JSON is limiting and that it’s a negative.  In my own opinion, I would say that the limits of what objects can be modeled in JSON is only to help make the code easy to read, more simple overall, and more predictable.

How to transfrom XML to JSON

To transform your XML to JSON using PHP, you’ll want to use a couple of built-in PHP functions.  If your data is not yet in XML format, might I suggest you get familiar with simplexml_load_string and/or simplexml_load_file.

If your data is retrieved from CCB and you’ve used our tutorials, then you have already transformed your data using SimpleXMLElement.  Okay, so now you have your XML.

Using the search for CCB individual codebase, we’re going to make slight modifications to it using the code below.

To transform your XML to JSON, use the json_encode method and this transforms your XML to a JSON encoded string.  If you desire to view the JSON encoded string as a PHP variable or array, then use the json_decode method.  Once decoded, you can print_r or var_dump the decoded JSON object to a page in web browser.

If you’re using some sort of DOM Manipulation Application or Javascript framework, you can simply make an AJAX call to the a PHP file containing logic to return a JSON encoded string.

So as you can see, it’s quite simple and easy to transform XML to JSON using PHP.  That’s it for this tutorial.  Let me know should you have questions.

 


Please comment if this tutorial has helped you.