Copyright © Cloud Software Group, Inc. All Rights Reserved
Copyright © Cloud Software Group, Inc. All Rights Reserved


Chapter 7 Transaction Pipeline Processing : JSON XML Transformation

JSON XML Transformation
TIBCO API Exchange Gateway can handle non-XML request and response messages, and provide XSLT extension functions to handle non-XML message serializations.
Using the mapping feature of TIBCO API Exchange Gateway, you can transform the messages as follows:
To support the JSON XML message transformations, TIBCO API Exchange Gateway provides the following:
JSON Extension
The JSON extension is defined in the namespace http://www.tibco.com/asg/functions/json
XSLT Functions
TIBCO API Exchange Gateway provides the following two functions:
The parse() function converts the JSON message format to XML message format.
The render() function converts the XML message format to JSON message format.
JSON Schema
TIBCO API Exchange Gateway provides the following built-in schema for JSON message:

 
<?xml version="1.0" encoding="UTF-8"?>
 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://www.tibco.com/asg/content-types/json"
targetNamespace="http://www.tibco.com/asg/content-types/json"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="json_xml">
<xs:complexType>
<xs:sequence>
<xs:element ref="json:list"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="list">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="json:text"/>
<xs:element ref="json:double"/>
<xs:element ref="json:list"/>
<xs:element ref="json:dict"/>
</xs:choice>
<xs:attribute name="key" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="text">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="key" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="double">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="key" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="dict">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="json:text"/>
<xs:element ref="json:double"/>
<xs:element ref="json:list"/>
</xs:choice>
<xs:attribute name="key" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>

 
Convert XML Message to JSON Message
By default, TIBCO API Exchange Gateway sends the response message in XML format from the target operation to the client. If a client requests the response message in a JSON format, you can convert the XML message to a JSON message as follows:
 
Task A Create an XSLT File
To create an XSLT file to render the data in JSON format, follow these high level steps:
The render() function requires the following namespace: http://www.tibco.com/asg/functions/json
Use the render() function
See Example XSLT to Convert BookQuery XML Response to JSON Response for an example XSLT.
Task B Upload the XSLT File
Using the Config UI, upload the XSLT file in the northbound reverse mapper to render the XML response message in JSON message format as follows:
1.
2.
3.
Add a new project or select an existing project under Projects. For example, select BookQuery project.
4.
a.
Click the MAPPING > Mapping tab.
b.
Click the Add property to create a new mapping.
c.
Mapping Name: XML_JSON_Mapping
Type:XSLT (select from the drop-down list.)
New File: Click Choose File and select the XSLT file.
Response Type:Full (select from the drop-down list.)
5.
6.
a.
Click the ROUTING > Facade Operations tab.
b.
c.
In the Response Transform field, select the XML_JSON_Mapping mapper, as created in the mapping configuration from the drop-down list.
7.
BookQuery Example Response Message
When the BookQuery example is run, TIBCO API Exchange Gateway sends the response message in XML format to the client, by default.
BookQuery Example XML Response
The response message in XML format is shown as follows:

 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<ns0:BookStore xmlns:ns0="http://www.example.com/xsd/books">
<ns0:Book>
<ns0:Title>The Power of Now</ns0:Title>
<ns0:Author>Vivek Ranadive</ns0:Author>
<ns0:Date>1999</ns0:Date>
<ns0:ISBN>0-06-566778-9</ns0:ISBN>
<ns0:Publisher>Tibco Software Inc</ns0:Publisher>
</ns0:Book>
</ns0:BookStore>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Transformed JSON Response for BookQuery Service:
{
"BookStore": {
"Book": [
{
"Publisher": "Tibco Software Inc",
"ISBN": "0-06-566778-9",
"Author": "Vivek Ranadive",
"Title": "The Power of Now"
}
]
}
}

 
 
Example XSLT to Convert BookQuery XML Response to JSON Response
Use the following XSLT to convert XML message format to JSON message format:

 
<xsl:stylesheet version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://www.example.com/xsd/books"
xmlns:fn="http://www.tibco.com/asg/functions/json"
xmlns:json="http://www.tibco.com/asg/content-types/json"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="xsl fn json ns0 SOAP-ENV">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="cnResponseHref">
<xsl:value-of select="/transformation/cnResponse/@href"/>
</xsl:variable>
<xsl:variable name="cnResponse">
<xsl:copy-of select="document($cnResponseHref)/*"/>
</xsl:variable>
<xsl:variable name="JsonXmlResponse">
<json:json_xml>
<json:dict>
<json:dict key="BookStore">
<json:list key="Book">
<xsl:for-each select="$cnResponse/SOAP-ENV:Envelope/SOAP-ENV:Body/ns0:BookStore/ns0:Book">
<json:dict key="Book">
<json:text key="Title"><xsl:value-of select="ns0:Title"/></json:text>
<json:text key="ISBN"><xsl:value-of select="ns0:ISBN"/></json:text>
<json:text key="Author"><xsl:value-of select="ns0:Author"/></json:text>
<json:text key="Publisher"><xsl:value-of select="ns0:Publisher"/></json:text>
</json:dict>
</xsl:for-each>
</json:list>
</json:dict>
</json:dict>
</json:json_xml>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="fn:render($JsonXmlResponse//json:json_xml/json:dict)"/>
</xsl:template>
</xsl:stylesheet>
 

 
BookQuery Example JSON Transformed Response
The following is the transformed JSON response message from the XSLT defined as Example XSLT to Convert BookQuery XML Response to JSON Response.

 
{
"BookStore": {
"Book": [
{
"Publisher": "Tibco Software Inc",
"ISBN": "0-06-566778-9",
"Author": "Vivek Ranadive",
"Title": "The Power of Now"
}
]
}
}

 
Convert JSON Message to XML Message
Using the northbound forward mapper, parse the JSON encoded string to generate an XML message. Usually, the JSON data is encoded using base64 format, therefore, the data must be converted from base64 to text. TIBCO API Exchange Gateway provides the codecs:base64ToText() function to convert the json data to text.
To parse a JSON encoded string in a north-side forward mapper, do the following:
 
Task A Create an XSLT File
To create an XSLT file to parse the JSON encoded, follow these steps:
The parse() function requires the following namespaces: http://www.tibco.com/asg/functions/json
http://www.tibco.com/asg/functions/codecs
Use the parse() function as follows:
Extract the base64 encoded request payload from the context document.
Use the codecs:base64ToText() function to convert the payload to text message.
Pass the text message to the json:parse() function.
See Example XSLT to Convert BookQueryBE JSON Request to XML Request for an example XSLT.
Task B Upload the XSLT File
Using the Config UI, upload the XSLT file in the northbound forward mapper to parse the JSON encoded request message in XML message format as follows:
1.
2.
3.
Add a new project or select an existing project under Projects. For example, select the BookQueryBE project.
4.
a.
Click the MAPPING > Mapping tab.
b.
Click the Add property to create a new mapping.
c.
Mapping Name: JSON_XML_Mapping
Type:XSLT (select from the drop-down list.)
New File: Click Choose File and select the XSLT file.
Response Type:Full (select from the drop-down list.)
5.
6.
a.
Click the ROUTING > Facade Operations tab.
b.
c.
In the Request Transform field, select JSON_XML_Mapping mapper, as created in the mapping configuration from the drop-down list.
7.
BookQueryBE Example Request Message
For example, when the BookQueryBE example is run, the client can send a request message in JSON format. This section describes how to convert the JSON data to an XML request using the json:parse() function for the BookQueryBE example.
BookQueryBE Example JSON Message
The client sends the following request message in JSON encoded string:

 
 
{
"BookStore": {
"Book": [
{
"Publisher": "Tibco Software Inc",
"ISBN": "0-06-566778-9",
"Author": "Vivek Ranadive",
"Title": "The Power of Now"
}
]
}
}
 

 
Example XSLT to Convert BookQueryBE JSON Request to XML Request
Use the following XSLT to convert the JSON message to an XML message format:

 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:j="http://www.tibco.com/asg/content-types/json"
xmlns:f="http://www.tibco.com/asg/content-types/form"
xmlns:m="http://www.tibco.com/asg/mapping"
xmlns:c="http://www.tibco.com/schemas/asg/context"
xmlns:h="http://www.tibco.com/asg/protocols/http"
xmlns:v="http://tag-pg.vipnet.hr/pg/content-types/formdata"
xmlns:form="http://www.tibco.com/asg/functions/form"
xmlns:json="http://www.tibco.com/asg/functions/json"
xmlns:codecs="http://www.tibco.com/asg/functions/codecs"
exclude-result-prefixes="xsl soapenv fn h c j f v form json" >
<xsl:variable name="context">
<xsl:for-each select="/transformation/context">
<xsl:copy-of select="document(@href)"/>
</xsl:for-each>
</xsl:variable>
 
<xsl:variable name="httpRequest">
<xsl:copy-of select="$context/c:context/c:entry[@key='asg:httpRequest']/h:request"/>
</xsl:variable>
 
<xsl:template match="/">
<xsl:if test="$httpRequest/h:request/h:body">
<xsl:copy-of select="json:parse(codecs:base64ToText($httpRequest/h:request/h:body))"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

 
 
BookQueryBE Example XML Transformed Request
The following is the transformed XML request message from the XSLT defined as Example XSLT to Convert BookQueryBE JSON Request to XML Request.

 
<?xml version="1.0" encoding="UTF-8"?>
<dict xmlns="http://www.tibco.com/asg/content-types/json">
<dict key="BookStore">
<list key="Book">
<dict>
<text key="Publisher">Tibco Software Inc</text>
<text key="ISBN">0-06-566778-9</text>
<text key="Author">Vivek Ranadive</text>
<text key="Title">The Power of Now</text>
</dict>
</list>
</dict>
</dict>

 
 

Copyright © Cloud Software Group, Inc. All Rights Reserved
Copyright © Cloud Software Group, Inc. All Rights Reserved