Adding responses as elements ,to an array in json response

Input request :

[
{
   "id" : "1",
"make" : "TAHB",
"children":[
{
"kid":"1"
},
{
"kid":"2"
}
]
},
{
"id" : "2",
"make" : "Tonda"
},
{
"id" : "3",
"make" : "Tamsung"
}
]


I am using iterate, to send each element of above array as request to a
backend service( In myactual project this is not so simple service i.e.
backend service reponse is very complex json with lots of arrays and
sub-arrays(child arrays)in it . for better understanding of issue I kept it
like this ).

The responses of the backend service are aggregated in one soap xml by
AggregateMediator.

Below is response from AggregateMediator

 <?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
<soapenv:Body>
<Information>
<jsonObject>
<id>1</id>
<make>TAHB</make>
<children>
<kid>1</kid>
</children>
<children>
<kid>2</kid>
</children>
<name>Home</name>
<area>5000sqft</area>
</jsonObject>
<jsonObject>
<id>2</id>
<make>Tonda</make>
<name>Car</name>
<model>Tmaze</model>
</jsonObject>
<jsonObject>
<id>3</id>
<make>Tamsung</make>
<name>Mobile</name>
<model>S8</model>
</jsonObject>
</Information>
</soapenv:Body>
</soapenv:Envelope>

Requirement is to add all the responses from the backend service except the
first one, to the child array of the first response of the backend service.
I do not know how to do this ? not even how to add new elements to a array
in wso2.

My Current Output :

{
"id": 1,
"make": "TAHB",
"children": [{
"kid": 1
},
{
"kid": 2
}],
"name": "Home",
"area": "5000sqft",
"jsonObject": [{
"id": 2,
"make": "Tonda",
"name": "Car",
"model": "Tmaze"
},
{
"id": 3,
"make": "Tamsung",
"name": "Mobile",
"model": "S8"
}]
}


Desired Output :

{
"id": 1,
"make": "TAHB",
"children": [
{
"kid": 1
},
{
"kid": 2
},
{
"id": 2,
"make": "Tonda",
"name": "Car",
"model": "Tmaze"
},
{
"id": 3,
"make": "Tamsung",
"name": "Mobile",
"model": "S8"
}
],
"name": "Home",
"area": "5000sqft",
}

Please find main xml and backendService xml attached. It contains what I
tried.
main xml : addChild.xml
backendService xml : modifyAgrRes.xml
<?xml version="1.0" encoding="UTF-8"?>
<api context="/addChild" name="addChild" xmlns="http://ws.apache.org/ns/synapse";>
    <resource methods="POST GET" uri-template="/hi">
        <inSequence>
            <log level="full"/>
            <log level="custom">
                <property expression="//jsonArray" name="message"/>
            </log>
            <property expression="//jsonArray" name="req" scope="default" type="STRING"/>
            <foreach expression="//jsonArray/jsonElement" id="Loop">
                <sequence>
                    <property expression="get-property('Loop_FOREACH_COUNTER')" name="countid" scope="default" type="STRING"/>
                    <log level="custom">
                        <property expression="get-property('countid')" name="mgs7"/>
                    </log>
                    <log description="" level="custom">
                        <property expression="get-property('Loop_FOREACH_ORIGINAL_MESSAGE')" name="mgs5"/>
                        <property expression="get-property('Loop_FOREACH_COUNTER')" name="mgs6"/>
                        <property expression="//jsonElement" name="msg8"/>
                    </log>
                    <log description="" level="custom">
                        <property expression="//jsonElement/data[0]" name="msg9"/>
                        <property expression="//jsonElement/data[1]" name="msg10"/>
                        <property expression="//jsonElement/data[2]" name="msg11"/>
                    </log>
                </sequence>
            </foreach>
            <iterate expression="//jsonArray/jsonElement" id="1" sequential="true">
                <target>
                    <sequence>
                        <log level="custom">
                            <property name="msg2" value="&quot;Inside iterate&quot;"/>
                        </log>
                        <log level="full"/>
                        <send>
                            <endpoint key="modifyAgrRespEP"/>
                        </send>
                    </sequence>
                </target>
            </iterate>
        </inSequence>
        <outSequence>
            <!--    <log level="full"/>  -->
            <property name="info" scope="default">
                <Information xmlns=""/>
            </property>
            <aggregate id="1">
                <completeCondition>
                    <messageCount max="-1" min="-1"/>
                </completeCondition>
                <onComplete enclosingElementProperty="info" expression="//jsonObject">
                    <log level="custom">
                        <property name="msg3" value="&quot;Inside Aggr&quot;"/>
                    </log>
                    <log level="full"/>
                    <foreach expression="//Information/jsonObject">
                        <sequence>
                            <log level="custom">
                                <property name="msg4" value="&quot;Inside Foreach&quot;"/>
                            </log>
                            <property expression="$body" name="agr" scope="default" type="STRING"/>
                            <log level="custom">
                                <property expression="get-property('agr')" name="agr"/>
                            </log>
                            <switch source="//jsonObject/id">
                                <case regex="1">
                                    <log level="custom">
                                        <property name="msg4" value="Inside Case1"/>
                                    </log>
                                    <property expression="//jsonObject" name="firstResponse" scope="default" type="OM"/>
                                </case>
                                <default>
                                    <log level="custom">
                                        <property name="msg4" value="Inside default"/>
                                    </log>
                                    <property expression="//jsonObject" name="aggrementResponse" scope="default" type="OM"/>
                                    <enrich>
                                        <source clone="true" xpath="$body//jsonObject"/>
                                        <target action="child" xpath="$ctx:firstResponse"/>
                                    </enrich>
                                </default>
                            </switch>
                            <log level="custom">
                                <property expression="get-property('aggrementResponse')" name="aggrementResponse"/>
                            </log>
                            <log level="custom">
                                <property expression="get-property('firstResponse')" name="firstResponse"/>
                            </log>
                        </sequence>
                    </foreach>
                    <enrich>
                        <source clone="true" xpath="get-property('firstResponse')"/>
                        <target type="body"/>
                    </enrich>
                    <send/>
                </onComplete>
            </aggregate>
        </outSequence>
        <faultSequence/>
    </resource>
</api>
<?xml version="1.0" encoding="UTF-8"?>
<api context="/mod" name="modifyAgrRes" xmlns="http://ws.apache.org/ns/synapse";>
    <resource methods="POST GET" uri-template="/aggr">
        <inSequence>
            <log level="custom">
                <property name="message" value="&quot;Inside Modify Service *************************&quot;"/>
            </log>
            <log level="custom">
                <property expression="//jsonObject" name="location"/>
            </log>
            <switch source="//jsonObject/id">
                <case regex="1">
                    <enrich>
                        <source clone="true" type="inline">
                            <name xmlns="">Home</name>
                        </source>
                        <target action="child" xpath="//jsonObject"/>
                    </enrich>
                    <enrich>
                        <source clone="true" type="inline">
                            <area xmlns="">5000sqft</area>
                        </source>
                        <target action="child" xpath="//jsonObject"/>
                    </enrich>
                </case>
                <case regex="2">
                    <enrich>
                        <source clone="true" type="inline">
                            <name xmlns="">Car</name>
                        </source>
                        <target action="child" xpath="//jsonObject"/>
                    </enrich>
                    <enrich>
                        <source clone="true" type="inline">
                            <model xmlns="">Amaze</model>
                        </source>
                        <target action="child" xpath="//jsonObject"/>
                    </enrich>
                </case>
                <case regex="3">
                    <enrich>
                        <source clone="true" type="inline">
                            <name xmlns="">Mobile</name>
                        </source>
                        <target action="child" xpath="//jsonObject"/>
                    </enrich>
                    <enrich>
                        <source clone="true" type="inline">
                            <model xmlns="">S8</model>
                        </source>
                        <target action="child" xpath="//jsonObject"/>
                    </enrich>
                </case>
                <default/>
            </switch>
            <log level="custom">
                <property expression="//jsonObject" name="msg20"/>
            </log>
            <enrich>
                <source clone="true" xpath="//jsonObject"/>
                <target type="body"/>
            </enrich>
            <!-- <enrich>
                <source clone="true" type="inline">
                    <name xmlns="">Y</name>
                </source>
                <target action="child" xpath="//jsonElement/data[2]"/>
            </enrich>
            <enrich>
                <source clone="true" property="countid" type="property"/>
                <target xpath="//jsonElement/data[2]/uniqueId"/>
            </enrich> -->
            <log level="full"/>
            <respond/>
        </inSequence>
        <outSequence>
            <send/>
        </outSequence>
        <faultSequence/>
    </resource>
</api>
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to