fb
WSO2 Enterprise Integrator 12 min

WSO2 EI / WSO2 ESB JSON Array Support

Wouter van Wijngaarden
Wouter van Wijngaarden
Integration Consultant
Laptop with code WSO2 EI WSO2 ESB Jason scaled
Scroll

Laptop with code - WSO2 EI WSO2 ESB JasonIn my previous blog about JSON object support on the WSO2 EI/ WSO2 ESB product, I showed some examples on how the product deals with JSON objects internally. In this blog I want to expand on that subject by showing an example with a JSON array and in addition show how create JSON messages on the product using the XSLT mediator. For this blog I will use an eclipse ESB solution project with the below API setup for the first examples.

Example setup       

<api name="testAPI" context="/transform">
   <resource methods="POST" uri-template="/example1">
      <inSequence>
         <log level="full"/>
         <respond/>
      </inSequence>
      <outSequence/>
      <faultSequence/>
   </resource>
   <resource methods="POST" uri-template="/example2">
      <inSequence>
         <property name="messageType" value="application/json" scope="axis2" type="STRING"/>
         <log level="full"/>
         <respond/>
      </inSequence>
      <outSequence/>
      <faultSequence/>
   </resource>
</api>

                      
Compared to the previous blog where I just sent a JSON object into the API I will instead start with a JSON array. The JSON array can be identified by the opening and closing brackets [ ] I’ve added several several jsonObjects to it.

Example request 1

{
"Objects":[{
    "testObject": {
        "testValue1": "value",
        "testValue2": "value"
    },
     "testObject2": {
        "testValue1": "value",
        "testValue2": "value"
    },
     "testObject3": {
        "testValue1": "value",
        "testValue2": "value"
    }
    }]
}

When I send a POST request of this to the API and check out the logs I find the below message. Just like before the ESB creates a jsonObject around the message to designate the surrounding jsonObject but there is no special element for the JSON array.

Example 1 during mediation inside the product

<jsonObject>
                  <Objects>
                                    <testObject>
                                                      <testValue1>value</testValue1>
                                                      <testValue2>value</testValue2>
                                    </testObject>
                                    <testObject2>
                                                      <testValue1>value</testValue1>
                                                      <testValue2>value</testValue2>
                                    </testObject2>
                                    <testObject3>
                                                      <testValue1>value</testValue1>
                                                      <testValue2>value</testValue2>
                                    </testObject3>
                  </Objects>
</jsonObject>

The result we get back is exactly the same JSON message as the input. So the bus manages to retain knowledge of it being an array even without a special xml element around it. 

Next we will try the same going from xml to json through the product.
As this input message I will use the xml message I showed above, which I took directly from the wso2carbon.log and send it to the example 2 api url “/transform/example2”.

Example request 2

<jsonObject>
    <Objects>
        <testObject>
            <testValue1>value</testValue1>
            <testValue2>value</testValue2>
        </testObject>
        <testObject2>
            <testValue1>value</testValue1>
            <testValue2>value</testValue2>
        </testObject2>
        <testObject3>
            <testValue1>value</testValue1>
            <testValue2>value</testValue2>
        </testObject3>
    </Objects>
</jsonObject>

 

Example 2 output message

{
    "Objects": {
        "testObject": {
            "testValue1": "value",
            "testValue2": "value"
        },
        "testObject2": {
            "testValue1": "value",
            "testValue2": "value"
        },
        "testObject3": {
            "testValue1": "value",
            "testValue2": "value"
        }
    }
}

As you can see the return message is missing the jsonArray and instead just turned into an object. This is because the original input message has no indication of any of the elements needing to be an array once it has been converted to JSON.

So, how can you make sure the received xml message is turned into the json result you’re looking for? The default JSON message formatter inside the ESB supports the <?xml-multiple?> tag, you can use this to designate the next element after to be an array once it’s been converted to JSON. If the origin system that sends the message is not able to add this element to the message it will have to be added to the message context during mediation on the bus.

For this we will add an extra resource call to the API with an Example3 API url /example3 and use the input from the example 2 request.

The new resource to add to the API

<resource methods="POST" uri-template="/example3">
      <inSequence>
         <property name="messageType" value="application/json" scope="axis2" type="STRING"/>
         <log level="full"/>
         <xslt key="gov:blog/xslt/example3.xslt"></xslt>
         <log level="full"/>
         <respond/>
      </inSequence>
      <outSequence/>
      <faultSequence/>
   </resource>

As you can see I’ve added an XSLT mediator call inside this API resource, we will have to create an XSLT file to add to the registry inside the project.

The XSLT file 

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns_xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/> 
    <xsl:template match="jsonObject"> 
        <jsonObject>
            <xsl:processing-instruction name="xml-multiple"/>         
            <xsl:copy-of select="Objects"></xsl:copy-of>
        </jsonObject>
    </xsl:template>
</xsl:stylesheet>

This xslt file does a simple conversion where we select the “jsonObject” element so we can add a new line inside it. You can do this by recreating the jsonObject element and then placing a copy of the original objects element inside it. Additionally place the  <xsl:processing-instruction name=”xml-multiple”/> line above the element that has to be converted to an array during transformation to JSON. This line adds the necessary <?xml-multiple?> line to the xml result. 

Now call the new /transform/example3 API resource with the request from example 2 and check out the result.

Inside the logs you will see the <?xml multiple?> line has been added.

Example 3 during mediation inside the product

<jsonObject><?xml-multiple ?><Objects>
        < testObject1>
                <testValue1>value</testValue1>
                <testValue2>value</testValue2>
        </ testObject1>
        < testObject2>
               <testValue1>value</testValue1>
               <testValue2>value</testValue2>
        </ testObject2>
        < testObject3>
               <testValue1>value</testValue1>
               <testValue2>value</testValue2>
        </ testObject3>
        </Objects>
</jsonObject>

Afterwards your result should look like the one below, it is an array as expected.

Example 3 result

{
    "Objects": [
        {
            "testObject1": {
                "testValue1": "value",
                "testValue2": "value"
            },
            "testObject2": {
                "testValue1": "value",
                "testValue2": "value"
            },
            "testObject3": {
                "testValue1": "value",
                "testValue2": "value"
            }
        }
    ]
}

This covers the basic handling of JSON arrays on the WSO2 EI and ESB products. For more information on JSON support of the WSO2 EI/ESB product check out the WSO2 documentation or leave a comment below. I will be happy to help you.

{{cta(‘d96cddee-168c-42a5-a0b6-00a670e766b7’)}}

Full API lifecycle Management Selection Guide

WHITEPAPER

smartmockups l0qqucke