DataPower

 View Only

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 4.0.2.2. -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:xalan="http://xml.apache.org/xalan"

xmlns:date="http://exslt.org/dates-and-times"

xmlns:dp="http://www.datapower.com/extensions"

xmlns:func="http://exslt.org/functions"

xmlns:ctvf="http://www.contivo.com/xslt/extensions" 

xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:v1="urn://EDP/AppBusinessLayer/Services/ArchiveRecord/v1"

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'"/>

               <soapenv:Envelope>

                       <soapenv:Header/>

                       <soapenv:Body>

                               <v1:CreateRequest>

                                      <Header/>

                                      <Record>

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

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

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

                                      </Record>

                               </v1:CreateRequest>

                       </soapenv:Body>

               </soapenv:Envelope>

        </xsl:template>

</xsl:stylesheet>

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="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" version="1.0" extension-element-prefixes="dp">        <xsl:template match="/">               <json:object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                        xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"                        xsi:schemaLocation="http://www.datapower.com/schemas/json 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: letakeda@gmail.com

Enjoy!



 

0 comments
69 views

Permalink