Thursday, June 10, 2010

How to convert XML to JSON

JSON is a very popular choice for transmitting data on the wire, especially since it is widely used for Apple iPhone app development. The usage of JSON allows a developer to skip the step in the browser where incoming XML must be parsed and read into an object. Instead, a serialized JavaScript object is sent, often including the data it needs already. So, no need for XML. But, in order to create this JSON structure in the first place, it is a good idea to offload this task onto an appliance such as an XML Gateway. The XML Gateway then serves up the JSON (to an iPhone for example), dynamically converting it from the source XML.

Here you can see XML being converted dynamically to JSON by an XML Gateway, as tested using the free SOAPbox tool:



This is enabled at the Gateway side by a relatively simple circuit. One key gotcha to remember is that when serving up JSON, make sure the content type is set to "application/json". Otherwise, clients may choke on it since they will not realize what it is.

Grab a copy of the Vordel Gateway to test this XML-to-JSON functionality here: http://vordel.com/products/vx_gateway/