JSON to XML Transformation

Version: 17.07

Supported Since: 17.01

Use case description

Cookie Mart is a retail biscuit seller that has several stores throughout the country. They recently bought and installed a stock management system in each store. When stocks are running low, this system automatically generates a re-order request and send to Cookie Mart's sole biscuit supplier, Happy Confectionery Limited.

This re-order request is generated as JSON and in the following format.

[
        {
                "id": "12024",
                "name": "Chocolate Cookies",
                "quantity": 1500
        },
        {
                "id": "60294",
                "name": "Spicy Crackers",
                "quantity": 4000
        },
        {
                "id": "36730",
                "name": "Butter Cookies",
                "quantity": 3200
        },
        {
                "id": "53912",
                "name": "Ginger Nuts",
                "quantity": 1800
        },
        {
                "id": "73741",
                "name": "Vanilla Wafers",
                "quantity": 2500
        }
]

The ordering system of Happy Confectionery Limited accepts orders through its HTTP web service interface, but unfortunately it can only understand the order requests in the XML format shown below. As a result Cookie Mart cannot directly use the JSON request generated by its stock management system to place orders to Happy Confectionery Limited.

<?xml version="1.0" encoding="UTF-8"?>
<hcl:order xmlns:hcl="http://hcl.biscuits">
   <hcl:product>
      <hcl:id>12024</hcl:id>
      <hcl:name>Chocolate Cookies</hcl:name>
      <hcl:quantity>1500</hcl:quantity>
   </hcl:product>
   <hcl:product>
      <hcl:id>60294</hcl:id>
      <hcl:name>Spicy Crackers</hcl:name>
      <hcl:quantity>4000</hcl:quantity>
   </hcl:product>
   <hcl:product>
      <hcl:id>36730</hcl:id>
      <hcl:name>Butter Cookies</hcl:name>
      <hcl:quantity>3200</hcl:quantity>
   </hcl:product>
   <hcl:product>
      <hcl:id>53912</hcl:id>
      <hcl:name>Ginger Nuts</hcl:name>
      <hcl:quantity>1800</hcl:quantity>
   </hcl:product>
   <hcl:product>
      <hcl:id>73741</hcl:id>
      <hcl:name>Vanilla Wafers</hcl:name>
      <hcl:quantity>2500</hcl:quantity>
   </hcl:product>
</hcl:order>

Proposed Solution

Since Cookie Mart cannot modify or replace their stock management system, they decided to introduce an ESB to transform the JSON order requests generated by their stock management system to the appropriate XML format.

json to xml transformation cookie mart arch

The stock management system of each store will send the JSON formatted order request to HTTP web service exposed by the ESB, and the ESB will transform the request to XML and forward to the web service of Happy Confectionery Limited's ordering system. Then the response received for that request will be proxied back to the stock management system that made the original request.

For this scenario assume that the ordering service of Happy Confectionery Limited is exposed over the URL http://localhost:9000/service/hcl/orderBiscuits which accepts HTTP POST requests with XML payload and responds with 200 (OK) status code if the request is successful, or 400 (Bad Request) if the request is invalid.

UltraStudio Configuration

UltraESB-X Configuration

Implementation of the Solution

Prerequisite

In order to implement above use case you must first select following dependencies when you are creating a new Ultra project.

  • HTTP NIO Connector from the connector list

  • Message Transformation from the processor list

If you have already created a project, you can add above dependencies via Component Registry. From Tools menu, select Ultra Studio → Component Registry and from the Connectors list and Processors list, select above dependencies.
Implementation

To implement above use case, first let’s create our integration flow named “cookie-mart-json-order-to-xml” and then add required processing components by going through following steps in order.

  1. Add a HTTP NIO Ingress Connector from the Connectors → Ingress Connectors list, to accept the JSON order requests from stores. HTTP NIO Ingress Connector should be configured as in below table to expose a single web service on port 8280 and under "/service/order-service" service path to accept order requests.

  2. Add a JSON to XML Transformer processing element from the Processors → Transformers list, to transform the JSON request to XML format to be sent to the Happy Confectionery Limited's service endpoint. It should be configured as in below table to retrieve the expected XML output based on the input JSON format. Connect the Processor outport of the previously added HTTP NIO Ingress Connector to the Input of this processing element.

  3. Finally add a HTTP NIO Egress Connector from the connectors Connectors → Egress Connectors list, and configure as in below table to send the transformed XML request to the Happy Confectionery Limited's service endpoint. Then connect the Next outport of the JSON to XML Transformer to the inport of this egress connector. Also connect the Response Processor outport of this egress connector back to the Input of the HTTP NIO Ingress Connector to send back the received response.

The completed integration flow should look like below.

json to xml transformation cookie mart flow

Configuration for each element is as below. The numbering corresponds to the numbers shown in above diagram.

Design View

Text View

.

1. HTTP NIO Ingress Connector

json to xml transformation component 1

2. JSON to XML Transformer

json to xml transformation component 2

3. HTTP NIO Egress Connector

json to xml transformation component 3
.

1.NIO HTTP Ingress Connector

Http Port

8280

Service Path

/service/order-service

2. JSON to XML Transformer

Root Element Name

order

Array Element Name

product

Namespace Prefix

hcl

Namespace URI

3.NIO HTTP Egress Connector

Destination Address Type

URL

Destination Host

localhost

Destination Port

9000

Service Path

/service/hcl/orderBiscuits

.

Now you can run the Ultra Project and check the functionality of the integration flow. Create an UltraESB Server run configuration and start it.

Property Configuration

When running the sample in the UltraESB-X distribution, you need to override the following properties in-order for the sample to work. The properties file is located at $ULTRA_HOME/conf/projects/json-to-xml-transformation/default.properties

Refer to Managing Project Properties documentation on how to override properties.

json-to-xml-flow.http-sender.servicePath

The service path of the backend HTTP service (default value is service/hcl/orderBiscuits)

json-to-xml-flow.http-sender.port

The port of the backend HTTP service (default value is 9000)

After that navigate to $ULTRA_HOME/bin directory. Next you can run the UltraESB-X distribution with following command to start the engine with this sample project deployed.

./ultraesbx.sh -sample json-to-xml-transformation

Testing the Integration Project

  1. Start an HTTP server instance on port 9000, which will expose an endpoint on the service path service/hcl/orderBiscuits. This endpoint should be able to accept POST requests with XML payloads and validate that XML against the following XSD schema. If the validation is successful, it should return 200 response code or 400 in case the validation failed.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://hcl.biscuits"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="order" type="hcl:orderType" xmlns:hcl="http://hcl.biscuits"/>
    <xs:complexType name="productType">
        <xs:sequence>
            <xs:element name="id" type="xs:integer"/>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="quantity" type="xs:double"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="orderType">
        <xs:sequence>
            <xs:element type="hcl:productType" name="product" maxOccurs="unbounded" minOccurs="0"
                        xmlns:hcl="http://hcl.biscuits"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>
If you are unable create such server, the XPR bundle for this sample has an integration flow which will mock this behaviour (see src/main/conf/hcl-order-system/mock-hcl-backend.xcml). You can add that integration flow either to this same project or to a separate project and deploy.
  1. Send a HTTP request containing a JSON payload of the above format to http://localhost:8280/service/order-service. (You can use the HTTP Client shipped with Ultra Studio Toolbox for this purpose). If the transformed XML conforms to the expected schema (you can use the sample JSON provided at the start of this document for this), 200 response code will be received or 400 will be received otherwise. If the transformation failed for some reason, 500 response code will be received.

  2. After the message is sent, view the message payload between the JSON to XML Transformer and the HTTP NIO Egress Connector to see the transformed payload into XML format.

In this topic
In this topic
Contact Us