WSO2 payload factory appending additional field with JSON request - json

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...

Related

Trying to create a rsyslog template that splits message into multiple fields in json format

I have the following template which works for me:
template(name="plain-syslog" type="list" option.json="on") {
constant(value="{")
constant(value="\"#timestamp\":\"") property(name="timereported" dateFormat="rfc3339")
constant(value="\",\"host\":\"") property(name="hostname")
constant(value="\",\"severity-num\":") property(name="syslogseverity")
constant(value=",\"facility-num\":") property(name="syslogfacility")
constant(value=",\"severity\":\"") property(name="syslogseverity-text")
constant(value="\",\"facility\":\"") property(name="syslogfacility-text")
constant(value="\",\"syslogtag\":\"") property(name="syslogtag")
constant(value="\",\"message\":\"") property(name="msg")
constant(value="\"}")
}
The message field looks as follows:
DROP IN=eth0 OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:00:00 SRC=5.5.5.5 DST=10.10.10.10 LEN=40 TOS=0x00 PREC=0x20 TTL=235 ID=28655 PROTO=TCP
I wanted to split the message field and present as json.
I figured out a line in SED but not sure how to use it in rsyslog template file:
echo DROP IN=eth0 OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:00:00 SRC=5.5.5.5 DST=10.10.10.10 LEN=40 TOS=0x00 PREC=0x20 TTL=235 ID=28655 PROTO=TCP| sed 's/^DROP/TYPE=DROP/;s/[^ ][^ ]*/"&",\n/g;s/=/": "/g'
The output in syslog should be:
"DROP",
"IN": "eth0",
"OUT": "",
"MAC": "00:00:00:00:00:00:00:00:00:00:00:00:00:00",
"SRC": "5.5.5.5",
"DST": "10.10.10.10",
"LEN": "40",
"TOS": "0x00",
"PREC": "0x20",
"TTL": "235",
"ID": "28655",
"PROTO": "TCP",
With jq, you can easily convert the text line into JSON format.
This approach has the advantage over sed that special characters that could otherwise damage the JSON file are automatically escaped
INPUT='DROP IN=eth0 OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:00:00 SRC=5.5.5.5 DST=10.10.10.10 LEN=40 TOS=0x00 PREC=0x20 TTL=235 ID=28655 PROTO=TCP'
conversion to JSON
jq -R '. / " " | map(. / "=" | {key:.[0], value:.[1]}) | from_entries' <<< "$INPUT"
Output
{
"DROP": null,
"IN": "eth0",
"OUT": "",
"MAC": "00:00:00:00:00:00:00:00:00:00:00:00:00:00",
"SRC": "5.5.5.5",
"DST": "10.10.10.10",
"LEN": "40",
"TOS": "0x00",
"PREC": "0x20",
"TTL": "235",
"ID": "28655",
"PROTO": "TCP"
}
conversion to text line
jq -Rr '. / " " | map(. / "=" | "\"" + join("\": \"") + "\"") | join(", ")' <<< "$INPUT"
Output
"DROP", "IN": "eth0", "OUT": "", "MAC": "00:00:00:00:00:00:00:00:00:00:00:00:00:00", "SRC": "5.5.5.5", "DST": "10.10.10.10", "LEN": "40", "TOS": "0x00", "PREC": "0x20", "TTL": "235", "ID": "28655", "PROTO": "TCP"

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>

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.