Tuesday, August 18, 2015

How to Split a JSON array in WSO2 ESB

Say you have this JSON array.

Now you want to split users, and call some backend service for each user. Here is how to do. (Here I'm logging each user details instead of calling a backend service.)

Output will look like this.

[2015-08-18 15:49:27,200]  INFO - LogMediator Name is = X, Age is = 10
[2015-08-18 15:49:27,204]  INFO - LogMediator Name is = Y, Age is = 12
[2015-08-18 15:49:27,205]  INFO - LogMediator Name is = Z, Age is = 15

Hope this will be helpful to someone.



  1. Hi,
    This works very well, but i have a problem with a little difference.
    My JSONArray is a result of a DataService call (i have installed a DSS in a AS)
    I make a call from an ESB like this:
    '<payloadFactory media-type="xml">
    <p:valoresReport xmlns:p="ReportsDataService">
    <xs:uuid xmlns:xs="ReportsDataService">$1</xs:uuid>
    <arg value="123456789" />
    <call blocking="true">
    <endpoint key="ReportsDataServiceEndPoint" />

    The Response i read:
    <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
    <property expression="json-eval($.ReportRowSet.reportRow)" name="rows" />

    and then i log:
    <log level="custom">
    <property name="ROWS" expression="get-property('rows')" ></property>

    the output is:
    [2016-05-31 11:02:40,170] INFO - LogMediator ROWS = [{"column1":1,"column3":3,"column2":2},{"column1":"columna 1","column3":"columna 3","column2":"olumna 2"}]

    So i need to iterate over this array, but your aproach does not work for me

    <iterate continueParent="true" sequential="true" expression="//ReportRowSet/reportRow" id="MyIterator">
    <log level="custom">
    <property name="COLUMNA 1" expression="//reportRow/column1"></property>

    and i dont know what am i doing wrong
    Thanks in advance

    1. What is your complete response json?

  2. Hi Bhathiya,
    I need to insert this users array to the mysql DB using Dataservice. Can you explain this sample using simple Dataservice.

    Thank you.

    1. Hi,

      You can create your payload (payload for DSS user input resource) using payload factory and use "send" or "call" mediator to send this to DSS endpoint.

  3. Hi Bhathiya,
    I need expand the ENTITY_EXPANSION_LIMIT to recibe an json message you can help me?

    1. Try starting ESB with "entityExpansionLimit" parameter like this.

      ./wso2server.sh -DentityExpansionLimit=100000

      Or you can set the parameter inside wso2server.sh too.

  4. Hi Bhathiya,
    I'm having an issue related to JSONPath with filtering (eg : json-eval($.content[?(@.hotelcode='ALE1_LON')])) is not working properly with arrays in WSO2 ESB 5. Can you give an comment on that too? I have mentioned this in SOF also (link : http://stackoverflow.com/questions/42652896/jsonpath-with-filtering-is-not-working-properly-in-wso2-esb-5)

    1. I got the answer. Issue was with json-eval expression. json-eval($.content[?(@.hotelcode=='ALE1_LON')])) works fine. (it is ==. not =). Thanx

  5. Hi bhathiya,

    Awesome blog. Keep your works up