How can I get the response body in WSO2 ESB - json

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>

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"/>

Aggregate multiple json responses with aggregate mediator in wso2 esb

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>

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.

WSO2 payload factory appending additional field with JSON request

I am creating a JSON payload in WSO2 ESB. But recently I have noticed while hitting the endpoint, it is adding some extra field at the start and end of the message body. Thus the JSON structure is getting changed and as result failing to generate actual result. The extra field can not be seen from normal logs but if you enable http wire log, you can notice these fields. I am pasting the code below and also the wire logs.
<payloadFactory media-type="json">
<format>
{
"ExtReference": "$8",
"MemberSubType":{
"MemberSubTypeId":"20",
"Name":"DD"},
"Region": "",
"PersonalDetails": {
"FirstName": "$9",
"LastName": "sample",
"MiddleName": "",
"DateOfBirth": "",
"GenderType": "",
"SalutationType":"",
"MemberStatus":""
},
"Address": [{
"AddressLine1": "$1",
"AddressLine2": "$2",
"HouseName": "",
"HouseNumber": "",
"Street": "",
"Locality": "",
"City": "$3",
"County": "",
"Zip": "$15",
"CountryId": "NZ",
"PostBox": "",
"PostBoxNumber": ""
}],
"ContactDetails": [{
"Email": "$5",
"Phone": "$10",
"MobilePhone": "$7",
"Fax": "$6",
"ContactDetailsType": ""
}],
"ExtensionData": [{
"PropertyName":"",
"PropertyValue":""
}]
}
</format>
<args>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:addr1/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:addr2/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:city/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:country/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:email/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:fax/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:mobile/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:nSInternalID/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:name/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:phone/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:salesRep/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:state/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:territory/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:url/text()"/>
<arg evaluator="xml"
expression="//cus:addParentCustToJADERequest/cus:parentCustData/cus:zip/text()"/>
</args>
</payloadFactory>
<property name="messageType"
value="application/json"
scope="axis2"
type="STRING"/>
<log separator=", ****JSON_Request****">
<property name="toJSON" expression="json-eval($.)"/>
</log>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:8281/scholasticapi/member"/>
<property name="Authorization"
value="78ca6121-763c-41c6-8dfe-d73e761b9989;https://uat.snipp.ie/scholasticapi/member;Fri Jul 24 2015 15:03:02 GMT+0530 (India Standard Time);idiuOEsgiVQU2cJj2p2nawCEHGtyN1cIWzpHP+NlJm4="
scope="transport"/>
<property name="Content-Type" value="application/json" scope="transport"/>
</endpoint>
</call>
while it is hitting the endpoint I logged the request enabling the wire.
[2015-07-30 19:15:23,854] DEBUG - wire << "[\r][\n]"
[2015-07-30 19:15:23,855] DEBUG - wire << "44d[\r][\n]"
[2015-07-30 19:15:23,855] DEBUG - wire << "{[\n]"
[2015-07-30 19:15:23,856] DEBUG - wire << " "ExtReference": "4545",[\n]"
[2015-07-30 19:15:23,856] DEBUG - wire << " "MemberSubType":{[\n]"
[2015-07-30 19:15:23,856] DEBUG - wire << "[0x9][0x9][0x9]"MemberSubTypeId":"20",[\n]"
[2015-07-30 19:15:23,861] DEBUG - wire << "[0x9][0x9][0x9]"Name":"DD"},[\n]"
[2015-07-30 19:15:23,861] DEBUG - wire << " "Region": "", [\n]"
[2015-07-30 19:15:23,862] DEBUG - wire << " "PersonalDetails": {[\n]"
[2015-07-30 19:15:23,862] DEBUG - wire << " "FirstName": "arisan",[\n]"
[2015-07-30 19:15:23,862] DEBUG - wire << " "LastName": "sample",[\n]"
[2015-07-30 19:15:23,863] DEBUG - wire << " "MiddleName": "",[\n]"
[2015-07-30 19:15:23,863] DEBUG - wire << " "DateOfBirth": "",[\n]"
[2015-07-30 19:15:23,863] DEBUG - wire << " "GenderType": "",[\n]"
[2015-07-30 19:15:23,863] DEBUG - wire << " "SalutationType":"",[\n]"
[2015-07-30 19:15:23,864] DEBUG - wire << " "MemberStatus":""[\n]"
[2015-07-30 19:15:23,864] DEBUG - wire << " },[\n]"
[2015-07-30 19:15:23,864] DEBUG - wire << " "Address": [{[\n]"
[2015-07-30 19:15:23,865] DEBUG - wire << " "AddressLine1": "vfhd",[\n]"
[2015-07-30 19:15:23,865] DEBUG - wire << " "AddressLine2": "jdfvbgkjfds",[\n]"
[2015-07-30 19:15:23,865] DEBUG - wire << " "HouseName": "",[\n]"
[2015-07-30 19:15:23,865] DEBUG - wire << " "HouseNumber": "",[\n]"
[2015-07-30 19:15:23,866] DEBUG - wire << " "Street": "",[\n]"
[2015-07-30 19:15:23,866] DEBUG - wire << " "Locality": "",[\n]"
[2015-07-30 19:15:23,866] DEBUG - wire << " "City": "kolkata",[\n]"
[2015-07-30 19:15:23,866] DEBUG - wire << " "County": "",[\n]"
[2015-07-30 19:15:23,867] DEBUG - wire << " "Zip": "43543",[\n]"
[2015-07-30 19:15:23,867] DEBUG - wire << " "CountryId": "NZ",[\n]"
[2015-07-30 19:15:23,867] DEBUG - wire << " "PostBox": "",[\n]"
[2015-07-30 19:15:23,867] DEBUG - wire << " "PostBoxNumber": ""[\n]"
[2015-07-30 19:15:23,868] DEBUG - wire << " }],[\n]"
[2015-07-30 19:15:23,868] DEBUG - wire << " "ContactDetails": [{[\n]"
[2015-07-30 19:15:23,868] DEBUG - wire << " "Email": "sdjk#hs.com",[\n]"
[2015-07-30 19:15:23,868] DEBUG - wire << " "Phone": "378654839674",[\n]"
[2015-07-30 19:15:23,869] DEBUG - wire << " "MobilePhone": "87425343287",[\n]"
[2015-07-30 19:15:23,869] DEBUG - wire << " "Fax": "345435",[\n]"
[2015-07-30 19:15:23,869] DEBUG - wire << " "ContactDetailsType": ""[\n]"
[2015-07-30 19:15:23,869] DEBUG - wire << " }],[\n]"
[2015-07-30 19:15:23,870] DEBUG - wire << " "ExtensionData": [{[\n]"
[2015-07-30 19:15:23,870] DEBUG - wire << "[0x9][0x9][0x9]"PropertyName":"", [\n]"
[2015-07-30 19:15:23,870] DEBUG - wire << "[0x9][0x9][0x9]"PropertyValue":""[\n]"
[2015-07-30 19:15:23,870] DEBUG - wire << "[0x9][0x9][0x9]}][\n]"
[2015-07-30 19:15:23,871] DEBUG - wire << "}[\r][\n]"
[2015-07-30 19:15:23,871] DEBUG - wire << "0[\r][\n]"
[2015-07-30 19:15:23,871] DEBUG - wire << "[\r][\n]"
See in the 2nd line of the logs shows a value 44d and in the 2nd last line a value zero appended which has not been served anywhere in payload. I have confirmed from the system owner, that for these two junk values only the request is getting failed. Kindly suggest a way out to get reed of these two fields. Thanks in advance.
This zero at the end is related to the way the payload is returned to the client, which is the "chunked" transfer.
( See: https://en.wikipedia.org/wiki/Chunked_transfer_encoding)
In short, the 44d is simply the number of bytes that is part if this "chunk". The last "0" implies that there are no more chunks.
You can use: <property name="DISABLE_CHUNKING" value="true" scope="axis2"/> before sending your request to the backend server.
This will work fine...