Aggregate multiple json responses with aggregate mediator in wso2 esb - json

I have defined a api in wso2 esb and it calls two internal APIs through recepient list and which are passing json responses as follows.(sample responses)
{
"name": "api1",
"response": "success",
"status": "1"
}
and
{
"name": "api2",
"response": "unsuccess",
"status": "2"
}
I need to pass the response by aggregating both of these responses as a single response. I red about payloadfactory and able to construct aggregated response. But i need to aggregate whatever the responses coming from these 2 apis and generate response as one single json object and pass by including both of these responses as follows
{
"response1": {
"name": "api1",
"response": "success",
"status": "1"
},
"response2": {
"name": "api2",
"response": "unsuccess",
"status": "2"
}
}
so how can a accomplish with WSO2ESB. I'm using latest version of ESB.

I have created three APIs and aggregated the API responses using Clone ,Below is my API which is used for aggregating responses of two API endpoints
<api xmlns="http://ws.apache.org/ns/synapse" name="aggregateResponse" context="/aggregate">
<resource methods="POST">
<inSequence>
<clone id="aggr">
<target>
<sequence>
<call>
<endpoint>
<http method="GET" uri-template="http://localhost:8280/getresponse1"/>
</endpoint>
</call>
<log>
<property name="Logger1" expression="json-eval($.)"/>
</log>
</sequence>
</target>
<target>
<sequence>
<call>
<endpoint>
<http method="GET" uri-template="http://localhost:8280/getResponse2"/>
</endpoint>
</call>
<log>
<property name="Logger1" expression="json-eval($.)"/>
</log>
</sequence>
</target>
</clone>
<payloadFactory media-type="json">
<format>{"responses":{ "name":"$1","response":"$2","status":"$3"}}</format>
<args>
<arg evaluator="json" expression="$.name"/>
<arg evaluator="json" expression="$.response"/>
<arg evaluator="json" expression="$.status"/>
</args>
</payloadFactory>
<loopback/>
</inSequence>
<outSequence>
<property name="res" scope="default">
<ResponseDetail xmlns=""/>
</property>
<aggregate id="aggr">
<completeCondition>
<messageCount min="-1" max="-1"/>
</completeCondition>
<onComplete expression="$body//responses" enclosingElementProperty="res">
<payloadFactory media-type="json">
<format>{"response1":$1 ,"response2":$2}</format>
<args>
<arg evaluator="json" expression="$.ResponseDetail.responses[0]"/>
<arg evaluator="json" expression="$.ResponseDetail.responses[1]"/>
</args>
</payloadFactory>
<send/>
</onComplete>
</aggregate>
</outSequence>
</resource>
</api>
API 1:
<api xmlns="http://ws.apache.org/ns/synapse" name="response1" context="/getresponse1">
<resource methods="GET">
<inSequence>
<payloadFactory media-type="json">
<format>{ "name": "api1", "response": "success", "status": "1"}</format>
<args/>
</payloadFactory>
<respond/>
</inSequence>
</resource>
</api>
API 2:
<api xmlns="http://ws.apache.org/ns/synapse" name="response2" context="/getResponse2">
<resource methods="GET">
<inSequence>
<payloadFactory media-type="json">
<format>{ "name": "api2", "response": "unsuccess", "status": "2"}</format>
<args/>
</payloadFactory>
<respond/>
</inSequence>
</resource>
</api>

Well, this is where enrich mediator becomes handy. Please try this out. I have not tested this since I am not doing WSO2 related stuffs now. But your feedback is warmly welcome. The pseudo code is something like this.
<call>
<endpoint>
<http method="GET" uri-template="http://www.mocky.io/v2/some-ep"/>
</endpoint>
</call>
<enrich>
<source type="body" clone="true"/>
<target type="property" property="first-json"/>
</enrich>
<call>
<endpoint>
<http method="GET" uri-template="http://www.mocky.io/v2/another-ep"/>
</endpoint>
</call>
<enrich>
<source type="property" property="first-json" clone="true"/>
<target action="sibling" xpath="//"/>
</enrich>
</respond>

Related

Concatenate two JSON responses in wso2 esb

I have two json from two different endpoints, i need to concatenate to convert it into a single valid json. I did it, but the first WSO2 json returns it as a string (firstjson). I would like it to be all a json.
FIRST JSON
{
"first": true,
"second": {
"name": "manoj",
"age": "45"
},
"third": {
"fourth": [
{
"class": "test12",
"salary": "123456"
},
{
"class": "test23",
"salary": "15678"
}
],
"fifth": "hello"
}
}
SECOND JSON
[
{
"item1": "123456",
"item2": "5678"
},
{
"item1": "8976",
"item2": "abcd"
}
]
XML API
<api context="/concat" name="concat" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" url-mapping="/concatenare">
<inSequence>
<call>
<endpoint>
<http method="get" uri-template="http://www.mocky.io/v2/56b2d88c13000057518945d4">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<enrich>
<source clone="false" type="body"/>
<target property="first-json" type="property"/>
</enrich>
<log level="custom">
<property expression="get-property('first-json')" name="First json"/>
</log>
<call>
<endpoint>
<http method="get" uri-template="http://www.mocky.io/v2/56b2d87d1300007c518945d3">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<log level="custom">
<property expression="get-property('first-json')" name="*********BEFOREEEEEEEE"/>
</log>
<!-- <enrich>
<source clone="false" property="first-json" type="property"/>
<target type="body"/>
</enrich> -->
<payloadFactory media-type="xml">
<format>
<completeJson xmlns="">
<firstjson>$1</firstjson>
<secondjson>$2</secondjson>
</completeJson>
</format>
<args>
<arg evaluator="xml" expression="get-property('first-json')"/>
<arg evaluator="xml" expression="$body"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<send/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
RESPONSE API
{
"completeJson": {
"firstjson": "{\"first\":true,\"second\":{\"name\":\"manoj\",\"age\":\"45\"},\"third\":{\"fourth\":[{\"class\":\"test12\",\"salary\":\"123456\"},{\"class\":\"test23\",\"salary\":\"15678\"}],\"fifth\":\"hello\"}}",
"secondjson": {
"Body": [
{
"item1": 123456,
"item2": 5678
},
{
"item1": 8976,
"item2": "abcd"
}
]
}
}
}
Where am I wrong ?
Thanks in advance guys
Ok guys,
I solved it, I was wrong with the payloadFactory, the format it had to be of type json.
<payloadFactory media-type="json">
<format>
{
"completeJson" : {
"firstjson" : $1,
"secondjson" : $2
}}
</format>
<args>
<arg expression="get-property('first-json')" />
<arg expression="$body" />
</args>
</payloadFactory>
thanks anyway guys :)

Transforming JSON array with WSO2

Do we have some idea how Transforming json with array for new json with another fields.
I have example json. This sample json is dynamic. Can have three or four elements in array.
"insurer": [
{
"data": {
"first_name": "Name",
"last_name": "SureName"
}
},
{
"data": {
"first_name": "Name1",
"last_name": "SureName1"
}
}],
And I'd like receive json
"insurer": [
{
"data": {
"name": "Name",
"nameLast": "SureName"
}
},
{
"data": {
"name": "Name1",
"nameLast": "SureName1"
}
}],
I did this sequence
<foreach id="foreach_1" expression="//insurer/data">
<sequence>
<payloadFactory media-type="json">
<format>{ "name" : "$1",
"nameLast" : "$2" }
</format>
<args>
<arg evaluator="xml" expression="//first_name"/>
<arg evaluator="xml" expression="//last_name"/>
</args>
</payloadFactory>
<log>
<property name="message" value="petla"/>
</log>
</sequence>
</foreach>
Unfortunately, I'm only getting a partial json. You may have an idea
{ ,"name" : "Name1", "nameLast" : "SureName1" }
I'm using WSO2 ESB V6.1.0
With 3 changes to your sequence, this should work as expected.
First change the expression of the foreach modiator to //insurer,
since your JSON message has array for insurer.
Next, use XML as the
media-type of the payload factory mediator and compose a single
expected insurer element.
Finally set messageType property to change the message type to JSON.
Find sample code below.
<foreach xmlns:ns="http://org.apache.synapse/xsd"
expression="//insurer">
<sequence>
<payloadFactory media-type="xml">
<format>
<insurer>
<data>
<name>$1</name>
<nameLast>$2</nameLast>
</data>
</insurer>
</format>
<args>
<arg evaluator="xml" expression="//first_name"/>
<arg evaluator="xml" expression="//last_name"/>
</args>
</payloadFactory>
</sequence>
</foreach>
<property name="messageType" value="application/json" scope="axis2"
type="STRING"/>

How can I get the response body in WSO2 ESB

The response format like this in WSO2 ESB:
<testresponse xmlns="http://abcd/service">
<cookie>yummy</cookie>
<product>YM11</product>
<place>US</place>
</testresponse >
But I just want the body, how can I do?
<cookie>yummy</cookie>
<product>YM11</product>
<place>US</place>
The goal is to transform the xml in a JSON object like the following
{
"cookie": "yummy",
"product": "YM11",
"place": "US"
}
You can use the payloadFactory
<payloadFactory media-type="xml">
<format>
<jsonObject>
<cookie>$1</cookie>
<product>$2</product>
<place>$3</place>
</jsonObject>
</format>
<args>
<arg evaluator="xml" expression="//cookie"/>
<arg evaluator="xml" expression="//product"/>
<arg evaluator="xml" expression="//place"/>
</args>
</payloadFactory>
But as already commented you need an enclosing element if not your xml will simply not be valid. If the goal is to output Json this element should be called jsonObject (find more info here : https://docs.wso2.com/display/EI600/JSON+Support).
If you don't need XML the best approach would be to directly build your json object
<payloadFactory media-type="json">
<format>
{
"cookie": $1,
"product": $2,
"place": $3
}
</format>
<args>
<arg evaluator="xml" expression="//cookie"/>
<arg evaluator="xml" expression="//product"/>
<arg evaluator="xml" expression="//place"/>
</args>
</payloadFactory>

Nested JSON in ODATA SAPUI5 mockup

I am using a nested JSON file as mockup data in a ODATA SAPUI5 application, but I cannot access the nested data.
JSON content
[{
"testcase": {
"specification": "SRS PR 28717 – Deposit in Brazilian Reais",
"execution": {
"description": "DESC",
"bca_cn_acct_01": {
"header": {
"section": "Field / Section Name",
"data": "Data to Enter / Value to Select",
"action": "Activity / Check / Comment"
},
"frame": {
"ID": 1,
"title": "Create Account: Initial Screen",
"data": [{
"key": "Contract Start",
"value": "02/16/2000",
"action": ""
}, {
"key": "Contract Manager",
"value": "GH_RAMOSCL",
"action": ""
}, {
"key": "Product",
"value": "BR_IOFTC3",
"action": ""
}, {
"key": "Account Holder",
"value": "GH_IOF_COR",
"action": "Press Enter"
}]
}
}
},
"result": "Teste"
}}]
In my view file, I want to access the data inside the tag, as a list:
<core:View controllerName="sap.ui.demo.MockServer.controller.App" xmlns:core="sap.ui.core" xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="sap.m">
<!-- <List headerText="{i18n>headerText}" id="list" items="{/Meetups}" noDataText="{i18n>noDataText}">
<items>
<ObjectListItem number="{ path: 'EventDate', type: 'sap.ui.model.type.DateTime', formatOptions: { style: 'medium' } }" title="{Title}">
<attributes>
<ObjectAttribute text="{Description}"/>
</attributes>
</ObjectListItem>
</items>
</List>
<Button press="onPressAction" text="{i18n>loadFirstItems}"></Button> -->
<List headerText="{i18n>headerText}" id="list" items="{/Meetups}" noDataText="{i18n>noDataText}">
<items>
<ObjectListItem number="{ path: 'EventDate', type: 'sap.ui.model.type.DateTime', formatOptions: { style: 'medium' } }"
title="{testcase/execution/description}">
<attributes>
<ObjectAttribute text="{specification}"/>
</attributes>
</ObjectListItem>
</items>
</List>
<Table id="table" items="{/Meetups}">
<columns>
<Column width="12em">
<Text text="Field / Section Name"/>
</Column>
<Column minScreenWidth="Tablet" demandPopin="true">
<Text text="Data to Enter / Value to Select"/>
</Column>
<Column minScreenWidth="Tablet" demandPopin="true">
<Text text="Activity / Check / Comment"/>
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<ObjectIdentifier title="{testcase/execution/bca_cn_acct_01/frame/data/key}" class="sapMTableContentMargin"/>
<!-- <Text text="{testcase/execution/bca_cn_acct_01/bca_dte_event_begin_d}" /> -->
<Text text="{testcase/execution/bca_cn_acct_01/frame/data/value}"/>
</cells>
</ColumnListItem>
</items>
</Table>
<Button press="onPressAction" text="{i18n>loadFirstItems}"></Button>
However, data from fields key and value are not retrieved as list, although they are an array inside the JSON data tag.
I use as base the sample tutorial from SAPUI5 library: https://sapui5.hana.ondemand.com/#docs/guide/7a78f1b707c248fd9ec53dcb5f10814c.html
And below you can find the metadata file I have been using:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<edmx:Edmx Version="1.0"
xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema Namespace="NerdMeetup.Models"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityType Name="Meetup">
<ComplexType Name="testcase">
<ComplexType Name="execution">
<!-- Create Payment Item -->
<ComplexType Name="bca_cn_acct_01">
<ComplexType Name="header">
<Property Name="section" Type="Edm.String" Nullable="false" />
<Property Name="data" Type="Edm.String" Nullable="false" />
<Property Name="action" Type="Edm.String" Nullable="false" />
</ComplexType>
<ComplexType Name="frame">
<Property Name="ID" Type="Edm.Int32" Nullable="false" />
<Property Name="title" Type="Edm.String" Nullable="false" />
<ComplexType Name="data">
<Property Name="key" Type="Edm.Int32" Nullable="false" />
<Property Name="value" Type="Edm.Int32" Nullable="false" />
<Property Name="action" Type="Edm.Int32" Nullable="true" />
</ComplexType>
</ComplexType>
<Property Name="bca_dte_event_begin_d" Type="Edm.String" Nullable="false" />
<Property Name="bca_dte_orgunit_cnmgt" Type="Edm.String" Nullable="false" />
<Property Name="fspr_prodext_y" Type="Edm.String" Nullable="false" />
<Property Name="bca_dte_bupa_acchold" Type="Edm.String" Nullable="false" />
</ComplexType>
</ComplexType>
<Property Name="specification" Type="Edm.String" Nullable="true" />
</ComplexType>
</EntityType>
<EntityContainer Name="NerdMeetups" m:IsDefaultEntityContainer="true">
<EntitySet Name="Meetups" EntityType="NerdMeetup.Models.Meetup" />
<FunctionImport Name="FindUpcomingMeetups" EntitySet="Meetups" ReturnType="Collection(NerdMeetup.Models.Meetup)" m:HttpMethod="GET" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Any ideas?
Hugs
I guess you can either bind the entire array:
{testcase/execution/bca_cn_acct_01/frame/data/}
Or bind an specific element of the array:
{testcase/execution/bca_cn_acct_01/frame/data/0/key}
You could also use a formatter to receive an array and concatenate all the keys of the array check the
Formatter SAPUI5 documentation

How to concatenate two JSON responses in wso2 esb

The two jsons i need to concatenate to convert it into a single valid json are :
{
"first": true,
"second": {
"name": "manoj",
"age": "45"
},
"third": {
"fourth": [{
"class": "test12",
"salary": "123456"
},
{
"class": "test23",
"salary": "15678"
}
],
"fifth": "hello"
}
}
and
[{
"item1": "123456",
"item2": "5678"
},
{
"item1": "8976",
"item2": "abcd"
}]
Is it possible to concatenate these two without using any jquery. I need something related to wso2 esb code. I tried using enrich and other mediators but no luck so far.
You can concat the jsons using WSO2 ESB Payload Factory mediator as follows,
<api xmlns="http://ws.apache.org/ns/synapse" name="ConcatAPI" context="/concat">
<resource methods="GET">
<inSequence>
<call>
<endpoint>
<http method="GET" uri-template="http://www.mocky.io/v2/56b2d88c13000057518945d4"/>
</endpoint>
</call>
<enrich>
<source type="body" clone="true"/>
<target type="property" property="first-json"/>
</enrich>
<log level="custom">
<property name="First json" expression="get-property('first-json')"/>
</log>
<call>
<endpoint>
<http method="GET" uri-template="http://www.mocky.io/v2/56b2d87d1300007c518945d3"/>
</endpoint>
</call>
<payloadFactory media-type="xml">
<format>
<completeJson xmlns="">
<firstjson>$1</firstjson>
<secondjson>$2</secondjson>
</completeJson>
</format>
<args>
<arg evaluator="xml" expression="get-property('first-json')"/>
<arg evaluator="xml" expression="$body"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Note that i have retrieved your jsons from mocked services from mocky.io web site.
Thanks.