简体   繁体   中英

WSO2 EI, Iterate through JSON Array

I'm creating simple REST API by WSO2 EI. I'm sending this json:

{
    "array": ["Hello", "this", "is", "an", "arrray", "1", 3, 4]
}

I want to iterate over each element in array and show them in logs.

I have created that kind of sequence for it:

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="for_each_test_seq_in" trace="enable" xmlns="http://ws.apache.org/ns/synapse">
    <property expression="json-eval($.array)" name="array"
    scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
    <log level="full">
        <property expression="get-property('array')" name="ARRAY" xmlns:ns="http://org.apache.synapse/xsd"/>
    </log>
    <iterate expression="get-property('array')" id="iterate_one" xmlns:ns="http://org.apache.synapse/xsd">
        <target sequence="anon">
            <sequence>
                <log level="full"/>
            </sequence>
        </target>
    </iterate>
    <respond/>
</sequence>

When I launch it, I get this error message:

[2017-09-19 16:06:40,236] [] ERROR - SynapseXPath Evaluation of the XPath expression get-property('array') resulted in an error
org.jaxen.UnresolvableException: No Such Function get-property
    at 
org.jaxen.SimpleFunctionContext.getFunction(SimpleFunctionContext.java:127)
    at org.jaxen.ContextSupport.getFunction(ContextSupport.java:242)
    at org.jaxen.Context.getFunction(Context.java:216)
    ...

I don't understand why get-property is unknown for it. I know that get-property is a XPath function.

I just want to iterate over each element of array element and do something with them.

Is it possible?

Here is implementation.

WSO API of rest service. Receive request, log body of message, process over sequence and send back to client. Sequence for_each_test_seq_in doesn't change content of message

<?xml version="1.0" encoding="UTF-8"?>
<api context="/array" name="array" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <log>
                <property expression="json-eval($.*)" name="==============="/>
            </log>
            <sequence key="for_each_test_seq_in"/>
            <loopback/>
        </inSequence>
        <outSequence>
            <send/>
        </outSequence>
        <faultSequence/>
    </resource>
</api>

This is sequence code

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="for_each_test_seq_in" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
    <property expression="json-eval($.array)" name="array" scope="default" type="STRING"/>
    <log level="full">
        <property expression="get-property('array')" name="ARRAY" xmlns:ns="http://org.apache.synapse/xsd"/>
    </log>
    <foreach expression="//array">
        <sequence>
            <log level="full"/>
        </sequence>
    </foreach>
</sequence>

Example of post request on picture underneath SoapUI请求响应

Log of server states

[2017-10-16 23:55:18,375] [EI-Core]  INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, =============== = [[34,234,"example",56.3,"mom"]]
[2017-10-16 23:55:18,379] [EI-Core]  INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, ARRAY = [34,234,"example",56.3,"mom"], Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject><array>34</array><array>234</array><array>example</array><array>56.3</array><array>mom</array></jsonObject></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,380] [EI-Core]  INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>34</array></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,381] [EI-Core]  INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>234</array></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,381] [EI-Core]  INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>example</array></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,381] [EI-Core]  INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>56.3</array></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,382] [EI-Core]  INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>mom</array></soapenv:Body></soapenv:Envelope>

First line is represented by

<log><property expression="json-eval($.*)" name="==============="/></log>
 ...=============== = [[34,234,"example",56.3,"mom"]]

Second log line is result of part of code in sequence

<log level="full">
    <property expression="get-property('array')" name="ARRAY"/>
</log>
  ....Direction: request, ARRAY = [34,234,"example",56.3,"mom"], Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject><array>34</array><array>234</array><array>example</array><array>56.3</array><array>mom</array></jsonObject></soapenv:Body></soapenv:Envelope>

As you can see it has two representations. One is in json format:

ARRAY = [34,234,"example",56.3,"mom"]

Second is in xml notation according rules described in https://docs.wso2.com/display/ESB500/JSON+Support (chapter XML representation of JSON payloads )

<?xml version='1.0' encoding='utf-8'?>
   <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
           <soapenv:Body>
               <jsonObject>
                   <array>34</array>
                   <array>234</array>
                   <array>example</array>
                   <array>56.3</array>
                   <array>mom</array>
           </jsonObject>
    </soapenv:Body>
</soapenv:Envelope>

Documentation state that it is possible to apply xpath on json payload if you consider json representation in xml. Using this just apply for each to get each part of array using xpath //array

As result original array, in xml representation, is spitted by each element of array and logged in console. You can see it in rest of logs

LogMediator To: /array, MessageID: urn:uuid:6...  Envelope: <soapenv:Body><array>34</array></soapenv:Body>
LogMediator To: /array, MessageID: urn:uuid:6.... Envelope: <soapenv:Body><array>234</array></soapenv:Body>
LogMediator To: /array, MessageID: urn:uuid:6...  Envelope: <soapenv:Body><array>example</array></soapenv:Body>
LogMediator To: /array, MessageID: urn:uuid:6.... Envelope: <soapenv:Body><array>56.3</array></soapenv:Body>
LogMediator To: /array, MessageID: urn:uuid:6.... Envelope: <soapenv:Body><array>mom</array></soapenv:Body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM