Converting JSON to XML -- why?


I think this should be useful to anyone who’s using an API that returns JSON data in the Apiant system.

As part of my work on assignment 501, I’m trying to integrate Discourse with Apiant. Discourse returns data from its APIs in JSON format, so Robert told me to convert that data into XML.

My question is, why do I need to do this? I think it’s because all of the Apiant components downstream from my API call to Discourse require XML – am I correct?


That’s correct, all modules in the assembly editor operate upon XML. Even ones that appear to operate on text and numbers are really using XML under the hood.

The Server-Side Script module in particular requires that the inlined code return an XML response, whether it is Java or PHP.

For the PHP output, two options exist. One is to convert the JSON to XML directly in the PHP. Another is to simply output a single XML node containing the JSON payload and use the Data Streams - Create from Text module to convert the JSON payload to an XML data stream.

For example, have the PHP emit “<root><![CDATA[JSON PAYLOAD HERE]]></root>”.

Wrap the JSON with CDATA so that any embedded XML special chars in the payload don’t cause trouble.

This is probably the easier approach vs. doing the conversion in PHP.

That makes sense regarding what’s going on under the hood.

That kind of makes sense regarding how to convert from JSON to XML – but the Data Streams - Create from Text won’t output the same XML that the Autotask JSON to XML conversion code would produce, correct? It seems like the Data Streams - Create from Text approach will only wrap the JSON in some XML?

The “Data Streams - Create from Text” module will convert JSON into valid XML, not just wrap it. For example, dictionary objects are translated into “<o>” nodes for “object”, arrays are converted into “<a>” nodes for “array”, etc.