This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data":[ | |
{ | |
"id":1, | |
"name":"abc", | |
}, | |
{ | |
"id":2, | |
"name":"efg", | |
}, | |
{ | |
"id":3, | |
"name":"hij", | |
} | |
] | |
} |
Lets say following is the resulting payload you want.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"info":[ | |
{ | |
"id":"IDABC1", | |
"classid":1, | |
"name":"ABC", | |
"university":"University of Moratuwa", | |
"department":"CSE" | |
}, | |
{ | |
"id":"IDEFG2", | |
"classid":2, | |
"name":"EFG", | |
"university":"University of Moratuwa", | |
"department":"CSE" | |
}, | |
{ | |
"id":"IDHIJ3", | |
"classid":3, | |
"name":"HIJ", | |
"university":"University of Moratuwa", | |
"department":"CSE" | |
} | |
] | |
} |
This payload contains, a root element "info" under which there is an array of objects having,
1. id element which is created using value of "name" element from original payload, converted to uppercase , value of "id" element from original payload, and the string "ID"
2. classid element which is the value of "id" element from the original payload
3. The "name" from original payload in uppercase.
4. university element which is hard-coded
5. department element which is also hard-coded
And you need to send this resulting payload to some endpoint in JSON format.
Inside foreach mediator I use script mediator to convert the name to uppercase. Then using payload factory mediator I create the payload. And outside the foreach mediator I use send mediator to send the resulting payload to the endpoint.
Here is the sequence.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<sequence name="my-in-seq"> | |
<!-- for each object under data element --> | |
<foreach expression="//data"> | |
<sequence> | |
<!-- set the "name" attribute of the object as property "temp" --> | |
<property expression="//data/name" name="temp"/> | |
<!-- Convert "name" value which can be retrieved from "temp" property to uppercase | |
and save it in "upperCaseName" property --> | |
<script language="js"><![CDATA[var name = mc.getProperty("temp"); | |
mc.setProperty("upperCaseName",name.toUpperCase());]]></script> | |
<!-- create the corresponding resulting object --> | |
<payloadFactory media-type="xml"> | |
<format> | |
<!-- let the root of the resulting payload be "info" --> | |
<info> | |
<!-- set 1st arg value as "id" element --> | |
<id>$1</id> | |
<!-- set 2nd arg value as "classid" element --> | |
<classid>$2</classid> | |
<!-- set 3rd arg value as "name" element --> | |
<name>$3</name> | |
<!-- For the following two elements we are hardcoding values --> | |
<university>University of Moratuwa</university> | |
<department>CSE</department> | |
</info> | |
</format> | |
<args> | |
<!-- The value evaluated from this will be set in place of $1 inside format | |
fn:concat('ID',get-property('uppercaseName'),//data/id) will concatenate the string "ID", | |
the value of the property "upperCaseName" and the "id" attribute of the object --> | |
<arg evaluator="xml" expression="fn:concat('ID',get-property('upperCaseName'),//data/id)"/> | |
<!-- The value evaluated from this will be set in place of $2 inside format | |
Like this we can use json evaluator too. The xml evaluator equivalent for this | |
would be <arg evaluator="xml" expression="//data/id"/>--> | |
<arg evaluator="json" expression="$.data.id"/> | |
<!-- The value evaluated from this will be set in place of $3 inside format --> | |
<arg evaluator="xml" expression="get-property('upperCaseName')"/> | |
</args> | |
</payloadFactory> | |
</sequence> | |
</foreach> | |
<!-- we want the resulting payload to be in JSON format --> | |
<property name="messageType" scope="axis2" value="application/json"/> | |
<send> | |
<!-- send the resulting payload to this endpoint --> | |
<endpoint> | |
<http method="POST" trace="disable" uri-template="http://localhost:8080/test"/> | |
</endpoint> | |
</send> | |
</sequence> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<api context="/mediatortest" name="mediatortest"> | |
<resource inSequence="my-in-seq" methods="POST" protocol="http" url-mapping="/mytest"/> | |
</api> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -v -X POST http://localhost:8280/mediatortest/mytest -H"Content-Type:Application/json" -d'{"data":[{"id":1,"name":"abc"},{"id":2,"name":"efg"},{"id":3, "name":"hij"}]}' |
[1] https://docs.wso2.com/display/ESB490/ForEach+Mediator
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi, what is the script you had inside the script mediator? Is it the same as in this post?
DeleteI didn't see your reply. Then I deleted my post. How ever finally I got this.Your explanation is nice. Keep it up.Thank you
DeleteThe best casinos to play at - Dr. Maryland
ReplyDeleteIn 안산 출장샵 fact, if 진주 출장안마 you're looking for the best slots, you should definitely look at the ones from top online 상주 출장안마 casinos. We have 군산 출장안마 listed some of 청주 출장마사지 the best casinos to play at