DataPower General

How to transform JSON to XML on DataPower

By Leandro Takeda posted Mon June 17, 2019 11:27 AM


How to transform JSON to XML on DataPower

By Leandro Takeda (Jun 2019)


During these days I had the need to transform a service from JSON to XML. I needed to expose my SOAP Service as JSON to my clients. This looks like a common scenario during these days, right? Where JSON REST services are becoming more common.

The magic, “__JSONASJSONX”

First, I created an MPGW to handle this, and the magic happens on the Multi-Protocol Gateway Policy:

In the first rule, the match one, there are no secret, I´m just matching the POST method:

In the second Action, which is a Transform, is where the magic happens, you need to add as input the word: “__JSONASJSONX”, see screenshot:

And this is my XSLT code:

<?xml version="1.0" encoding="UTF-8"?>

<!-- IMPORTANT: If this transform will be run using Xalan, then version 2.5.1 or higher is required. -->

<!-- Copyright (c) 2008 Contivo, Inc. All Rights Reserved.-->

<!-- NOTE: DataPower supports the Xalan:nodeset() function. -->

<!-- This transform was created by Analyst version -->

<xsl:stylesheet version="1.0" xmlns:xsl=""








extension-element-prefixes="func ctvf date dp" exclude-result-prefixes="date dp">

<xsl:output omit-xml-declaration="yes" indent="yes"/>

        <xsl:template match="/">

        <dp:set-http-request-header name="'Content-Type'" value="'application/xml'"/>







                                              <ContentRep><xsl:value-of select="//*[@name='contentRepository']"/></ContentRep>

                                              <ContentType><xsl:value-of select="//*[@name='contentType']"/></ContentType>

                                              <DocData><xsl:value-of select="//*[@name='documentData']"/></DocData>







You will see the XSLT is pretty simple, I´m just doing a match, setting some header, defining the namespaces and later, creating the XML request and adding the values from previous XML generated using XPATH: <xsl:value-of select="//*[@name='documentData']"/>


You will notice the output from “__JSONASJSONX” is an XML. This JSON as JSONX is an IBM creation, to transform JSON in an XML, to help you in these cases.

Now, what about the RESPONSE?

The response is pretty simple as well, but the difference, you will see one .xsl for that.

In the first XSLT code, you will create the XML in the format you want, but have in mind that It will later be transformed in a JSON response, so, create the XML but thinking in a JSON format, see the example:

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="" xmlns:dp="" xmlns:json="" version="1.0" extension-element-prefixes="dp">        <xsl:template match="/">               <json:object xmlns:xsi=""                        xmlns:json=""                        xsi:schemaLocation=" jsonx.xsd">                       <json:string name="status">                               <xsl:value-of select="//*[local-name() = 'eDescription']"/>                       </json:string>                       <json:string name="archiveDocId">                               <xsl:value-of select="//*[local-name() = 'ArchDocId']"/>                       </json:string>               </json:object>        </xsl:template></xsl:stylesheet>


And in the second transform action, do this:

This jsonx2json.xsl is used for that, it will transform your XML in JSON. You can check all the details, like, how is your XML format using the PROBE log. And that´s it, you will see your JSON response after that!


Screenshot from Postman tool

If you have any issue, of course, use the PROBE logs, it´s very helpful, and you can also drop me an email if you need: