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.

References
https://denuwanthi.wordpress.com/2015/06/03/wso2-esbaccess-an-array-defined-in-property-mediator/

9 comments:

  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">
    <format>
    <p:valoresReport xmlns:p="ReportsDataService">
    <xs:uuid xmlns:xs="ReportsDataService">$1</xs:uuid>
    </p:valoresReport>
    </format>
    <args>
    <arg value="123456789" />
    </args>
    </payloadFactory>
    <call blocking="true">
    <endpoint key="ReportsDataServiceEndPoint" />
    </call>'

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

    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">
    <target>
    <sequence>
    <log level="custom">
    <property name="COLUMNA 1" expression="//reportRow/column1"></property>
    </log>
    </sequence>
    </target>
    </iterate>

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

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

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

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

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

      ./wso2server.sh -DentityExpansionLimit=100000

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

      Delete
  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)

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

      Delete
  5. Hi bhathiya,

    Awesome blog. Keep your works up

    ReplyDelete