Nag
Nag

Reputation: 377

How to avoid jsonObject in JSON response in wso2esb 4.9.0

Am working with Wso2esb 4.9.0, I have a service which selects data from database and gives response in JSON format.

Proxy service:

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="GetMquestionMobile3.0"
       transports="https http"
       startOnLoad="true"
       trace="disable"
       statistics="enable">
   <description/>
   <target>
      <inSequence onError="fault">
         <property name="messageType" value="application/json" scope="axis2"/>
         <property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
         <property name="userid" expression="get-property('transport', 'userid')"/>
         <property name="ModifiedOn" expression="get-property('transport', 'ModifiedOn')"/>
         <property name="username" expression="get-property('transport', 'username')"/>
         <property name="password" expression="get-property('transport', 'password')"/>
         <property name="PartyBranchID"
                   expression="//FieldValue/text()"
                   scope="default"
                   type="STRING"/>
         <property name="usercode"
                   expression="fn:substring-before(get-property('username'),'|')"
                   scope="default"
                   type="STRING"/>
         <property name="clientid"
                   expression="fn:substring-after(get-property('username'),'|')"
                   scope="default"
                   type="STRING"/>
<payloadFactory media-type="xml">
            <format>
               <p:Getmquestions_Mobile xmlns:p="http://ws.wso2.org/dataservice">
                  <p:clientid>$1</p:clientid>
                  <p:modifiedon>$2</p:modifiedon>
               </p:Getmquestions_Mobile>
            </format>
            <args>
               <arg evaluator="xml" expression="get-property('clientid')"/>
               <arg evaluator="xml" expression="get-property('ModifiedOn')"/>
            </args>
         </payloadFactory>
         <send receive="MquestionMobile_Seq33.0">
            <endpoint>
               <address uri="http://localhost:9764/services/mquestions_DataService3.0/"
                        format="soap11">
                  <suspendOnFailure>
                     <errorCodes>101500,101501,101506,101507,101508,101503,50000</errorCodes>
                     <initialDuration>30</initialDuration>
                     <progressionFactor>1.0</progressionFactor>
                     <maximumDuration>300</maximumDuration>
                  </suspendOnFailure>
               </address>
            </endpoint>
         </send>
</inSequence>
      <outSequence onError="fault">
         <send/>
      </outSequence>
   </target>
</proxy>

Sequence:

<sequence xmlns="http://ws.apache.org/ns/synapse"
          name="MquestionMobile_Seq33.0"
          statistics="enable">
   <property name="messageType" value="application/json" scope="axis2"/>
   <property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
   <property name="HTTP_METHOD" value="POST" scope="axis2"/>
   <property xmlns:ns="http://org.apache.synapse/xsd"
             xmlns:s="http://ws.wso2.org/dataservice"
             name="Datalist"
             expression="//s:Datalist"
             scope="default"
             type="STRING"/>
   <property xmlns:ns="http://org.apache.synapse/xsd"
             xmlns:s="http://ws.wso2.org/dataservice"
             name="Total"
             expression="count(//s:Datalist)"
             scope="default"
             type="STRING"/>
<filter xmlns:ns="http://org.apache.synapse/xsd"
           xpath="get-property('Total')='0.0'">
      <then>
         <property name="RESPONSE" value="true" scope="default" type="STRING"/>
         <payloadFactory media-type="json">
            <format>
        {"ResponseJSON":{"Body":{"Datalist":"Not Found"},"Status":"404"}}
    </format>
            <args/>
         </payloadFactory>
         <send/>
      </then>
      <else>
         <property name="RESPONSE" value="true" scope="default" type="STRING"/>
         <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
         <payloadFactory media-type="json">
            <format>{"ResponseJSON" : {"Body" :$1,"Status" :"200","Total" :"$2"}}</format>
            <args>
                <arg evaluator="json" expression="$.Body"/>
                <arg evaluator="xml" expression="get-property('Total')"/>
            </args>
         </payloadFactory>
         <send/>
      </else>
   </filter>   
</sequence>

This service gives me the response as shown below

{"jsonObject":{"ResponseJSON" : 
    {"Body" :
    {"Datalist":[
    {"ComboValues":"Yes,No","QuestionType":0,"Tag":-1,"Question":"LogBooks","IsAVPT":-1,"QuestionId":989181535979317,"DataSize":-1.0,"ToolTip":-1,"IsFollowup":-1},
    {"ComboValues":"Yes,No","QuestionType":0,"Tag":null,"Question":"MasterRegister","IsAVPT":-1,"QuestionId":989181536700214,"DataSize":-1.0,"ToolTip":null,"IsFollowup":-1},
    {"ComboValues":null,"QuestionType":0,"Tag":null,"Question":"OthersRecords","IsAVPT":-1,"QuestionId":989181536732983,"DataSize":-1.0,"ToolTip":null,"IsFollowup":-1}]},"Status":"200","Total":"3.0"}}}

I don't want to see the jsonObject in my response, how can i avoid this in my JSON response and get the response as below

{"ResponseJSON" : 
        {"Body" :
        {"Datalist":[
        {"ComboValues":"Yes,No","QuestionType":0,"Tag":-1,"Question":"LogBooks","IsAVPT":-1,"QuestionId":989181535979317,"DataSize":-1.0,"ToolTip":-1,"IsFollowup":-1},
        {"ComboValues":"Yes,No","QuestionType":0,"Tag":null,"Question":"MasterRegister","IsAVPT":-1,"QuestionId":989181536700214,"DataSize":-1.0,"ToolTip":null,"IsFollowup":-1},
        {"ComboValues":null,"QuestionType":0,"Tag":null,"Question":"OthersRecords","IsAVPT":-1,"QuestionId":989181536732983,"DataSize":-1.0,"ToolTip":null,"IsFollowup":-1}]},"Status":"200","Total":"3.0"}}

DS Response:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <Body xmlns="http://ws.wso2.org/dataservice">
         <Datalist>
            <Question>LogBooks</Question>
            <QuestionId>989181535979317</QuestionId>
            <QuestionType>0</QuestionType>
            <ComboValues>Yes,No</ComboValues>
            <IsFollowup>-1</IsFollowup>
            <IsAVPT>-1</IsAVPT>
            <DataSize>-1.0</DataSize>
            <ToolTip>-1</ToolTip>
            <Tag>-1</Tag>
            <weightage>1.00</weightage>
         </Datalist>
         <Datalist>
            <Question>Master Register</Question>
            <QuestionId>989181536700214</QuestionId>
            <QuestionType>0</QuestionType>
            <ComboValues>Yes,No</ComboValues>
            <IsFollowup>-1</IsFollowup>
            <IsAVPT>-1</IsAVPT>
            <DataSize>-1.0</DataSize>
            <ToolTip/>
            <Tag/>
            <weightage>1.00</weightage>
         </Datalist>
         <Datalist>
            <Question>Others Records</Question>
            <QuestionId>989181536732983</QuestionId>
            <QuestionType>0</QuestionType>
            <ComboValues/>
            <IsFollowup>-1</IsFollowup>
            <IsAVPT>-1</IsAVPT>
            <DataSize>-1.0</DataSize>
            <ToolTip/>
            <Tag/>
            <weightage>1.00</weightage>
         </Datalist>
          </Body>
   </soapenv:Body>
</soapenv:Envelope>

Upvotes: 0

Views: 2220

Answers (2)

Krishantha Dinesh
Krishantha Dinesh

Reputation: 377

In side ESB mediator communications take place as SOAP messages. when you send JSON request or when ESB received JSON response what ESB does in convert that to xml / soap message by adding tag to prevent multi rooted xml. try to use

<property name="messageType" value="application/json" scope="axis2"/> 

before send / call. this will automatically remove your jsonObject from response. but if you want to convert response to xml from json then you need to manually modify using enrich mediator

make sure json formatter enabled in axis2.xml

Upvotes: 0

Sachini Rajapakse
Sachini Rajapakse

Reputation: 191

You can use an enrich mediator to remove jsonObject from response body like below.

<enrich>
    <source clone="true" xpath="$body//jsonObject//ResponseJSON"/>
    <target type="body"/>
</enrich>

You can add a log mediator after enrich mediator and check the changed body.

Hope this will help you.

Upvotes: 1

Related Questions