Convert JSON to XML
To convert JSON data to XML, a mashup or macro receives the JSON object as a string, converts the JSON string to an XML string and finally converts the XML string to an XML document.
In this example, the result of a mashable is a JSON object which is placed in a string variable, jsonString. This is converted to an XML string with <script> and the toXML method in the com.jackbe.presto.common.DataTransformer utility class in MashZone NextGen:
...
<variable name="jsonString" type="string"/>
<variable name="xmlStr" type="string"/>
<variable name="xmlDoc" type="document"/>
<!-- invoke web service that returns JSON -->
<invoke service="jsonOutput" operation="Invoke"
outputvariable="$jsonString" />
<!-- convert JSON string to XML string -->
<script type="text/javascript" inputvariables="jsonString"
outputvariable="xmlStr">
<![CDATA[
xmlStr = toJson(jsonString);
function toJson(str){
return Packages.com.jackbe.presto.common.DataTransformer.jsonToXml(str);
}
]]>
</script>
<!-- cast XML string to document -->
<assign fromvariable="$xmlStr" outputvariable="$xmlDoc" />
<!-- proceed with more EMML processing -->
...
Once the data is in XML as a string, it is converted to an XML document using <assign> and a document-type output variable. At this point, the data is in a form that the mashup or macro can use any EMML statement needed for any further processing.