How to use WSO2 ESB – Payload Factory Mediator to change JSON service output

Engineer, Tech Lead and Technopreneur

In this post I am going to show how to use mediate a simple JSON REST service using WSO2 ESB.

1. First let’s assume we have a JSON web service which returns following JSON output to the client.

{
   "name": "Chatura Dilan",
   "company": "WSO2",
   "user": "dilan"
}

2. But the client does not require the JSON output in the above format. He need to change the “user” to “username” as following.

{
   "name": "Chatura Dilan",
   "company": "WSO2",
   "username": "dilan"
}

3. In this case, we are going to use the ‘Payload Factory Mediator’ of WSO2 ESB to change the payload of the message.

You can host a sample service in any server which returns the output as following. Please make sure the Content Type of the JSON output is application/json

{
   "name": "Chatura Dilan",
   "company": "WSO2",
   "user": "dilan"
}

If you are using Apache PHP server you can create a test php file and add the following code there.

<!--?php
header('Content-type: application/json');
echo '{
 "name": "Chatura Dilan",
 "company": "WSO2",
 "user": "dilan"
}';

4. Now start the WSO2 ESB, login to the ESB and click on ‘API’ Menu in the ‘Service Bus’ tab

5. Click on ‘Add API’ to create a new API

6.  Add API Name and Context as Following and Click on ‘Add Resource’

7.  Now we are adding a ‘In Sequence’, the sequence which does the communicate between the server and the ESB.

Set Resource Method as GET and In Sequence as ‘Define Inline’. Click on the Create link

8. Now we need to send a message to the server to get the output. So we add a send mediator for that by selecting Add Child -> Core -> Send

9. In ‘Send Mediator’ window click on ‘Define Inline’ and click on ‘Add’ link

10. Select ‘Address Endpoint’ from the list

11. Add the server URL which returns the JSON as the Address

12. Click ‘Save & Close’

13 . Click ‘Update’ and ‘Save & Close’ again

14.  The XML view of ‘In Sequence’ as following

<inSequence xmlns="http://ws.apache.org/ns/synapse">
 <send>
 <endpoint>

<address uri="http://localhost/test/test1.php"></address>

 </endpoint>
 </send>
</inSequence>

15. Now it’s time to create the ‘Out Sequence’. Which returns the message from the ESB to Client. So we are going to mediate the out sequence here. Select the ‘Out Sequence’ and Click on Create.

16. Now add a ‘PayloadFactory’ mediator By selecting Add Child -> Transform -> PayloadFactory

17. Please add the Payload format and Arguments as following

18. Also create the send mediator by setting the Endpoint to none.

The XML code of the ‘Out Sequence’  will be following.

<outSequence xmlns="http://ws.apache.org/ns/synapse">
 <payloadFactory media-type="json">
 <format>{ "name": "$1", "company": "$2", "username": "$3"}</format>
 <args>
 <arg expression="$.name" evaluator="json"></arg>
 <arg expression="$.company" evaluator="json"></arg>
 <arg expression="$.user" evaluator="json"></arg>
 </args>
 </payloadFactory>
 <send></send>
</outSequence>

19. Now click ‘Update’ , ‘Save & Close’, ‘Update’ and ‘Save’

20. That’s it. Access the  API invocation URL and you can see the output message is mediated by the WSO2 ESB

http://localhost:8280/mytest

now you can see the output response is mediated using WSO2 ESB

{
   "name": "Chatura Dilan",
   "company": "WSO2",
   "username": "dilan"
}
 

5,887 total views, 1 views today

 

2 thoughts on “How to use WSO2 ESB – Payload Factory Mediator to change JSON service output

  1. Hello I did it on developer kit(ecslipse) and after sending the api url I got all value as null

    pfa of my xml

    {
    “id”:”$1″,
    “firstName”:”$2″,
    “lastName”:”$3″,
    “email”:”$4″,
    “mobile”:”$5″,
    “dateOfBirth”:”$6″
    }

     
  2. How to configure when response is a array json?
    I have a follow schema in json :
    [
    {
    “userId”: 1,
    “id”: 1,
    “title”: “sunt aut facere repellat provident occaecati excepturi optio reprehenderit”,
    “body”: “quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto”
    },
    {
    “userId”: 1,
    “id”: 2,
    “title”: “qui est esse”,
    “body”: “est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla”
    },

    ]

     

Feel Free to ask questions and share your thoughts