Ask a Jedi: How do I convert JSON to XML?

Jody asks:

Quick question... But probably long answer... Is there anyway possible to convert JSON to XML?

Tsk tsk, Jody. I find your lack of faith (in ColdFusion!) to be disturbing! ;) Actually it isn’t that difficult at all. The process I’d employ is to first convert the JSON into a native ColdFusion data structure and then convert that to XML. Here are a few examples. First, let’s talk about JSON conversion. In ColdFusion 8, this is a no-brainer. It’s just built in. So to convert some data into JSON I could do this:

<cfset s = {}> <cfset = "Raymond Camden"> <cfset s.age = 36> <cfset = ["Jacob","Lynn","Noah"]> <cfset s.handed = "left">

<cfset jsonVersion = serializeJSON(s)> <cfoutput>json version=#jsonVersion#<p></cfoutput> </code>

This returns: json version={“HANDED”:”left”,”NAME”:”Raymond Camden”,”AGE”:36.0,”KIDS”:[“Jacob”,”Lynn”,”Noah”]}

Now if we pretend we began with that JSON, converting it back into ColdFusion is as simple as:

<cfset data = deserializeJSON(jsonVersion)>

Note - if you don’t have ColdFusion 8, I recommend the CFJSON CFC for JSON conversion. It’s like butter.

Ok, so at this point we are back to having native ColdFusion data, how do we convert it to XML? Outside of WDDX support, there is no way to convert dynamic ColdFusion data into XML. I’ve got my own little CFC for that, toXML, but I never updated it to be recursive. I picked a random project from RIAForge, AnythingToXML, and decided to use that. It works ok, but note that it assumes you want to serve up the XML data instead of actually work with it. I manually edited the project’s CFC to remove the tag.

Anyway, after a few seconds looking at the docs, I was able to generate the XML like so:

<!--- then convert to xml ---> <cfset xmlcfc = createObject("component", "AnythingToXML.AnyThingToXml")> <cfset packet = xmlcfc.toXML(data,"data")>

<cfoutput>#htmlEditFormat(packet)#</cfoutput> </code>

Not really rocket science, but it works. Of course, you don’t have to use a component. If you know the form of your data, you can always build the XML manually. Consider the following template. It makes use of the Twitter API to get JSON data for a search. I then loop over the results and generate XML.

<cfset q = "coldfusion"> <cfhttp url="" result="result">

<cfset data = deserializeJSON(result.fileContent)>

<cfxml variable=”packet”> <twitterresults> <cfoutput> <query>#xmlFormat(q)#</query> <cfloop index=”r” array=”#data.results#”> <result> <user>#xmlFormat(r.from_user)#</user> <created>#xmlFormat(r.created_at)#</created> <text>#xmlFormat(r.text)#</text> </result> </cfloop> </cfoutput> </twitterresults> </cfxml>

<cfdump var=”#packet#”> </code>

The Twitter API returns more information than what I’ve used. I kept things simple though. Here is a quick screen shot showing the result:

One quick last note - well ok, two quick notes. First off - the Twitter API kicks major butt. I’m blown away with how simple it is to use and how - lord forbid - how much it helps you actually use it. (Google - are you listening? Please don’t buy Twitter and ruin the API.) Secondly - Twitter supports ATOM results. This is a flavor of RSS and is XML already, so technically, I wouldn’t need to convert the JSON.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA