简体   繁体   English

WSO2 EI For通过Json数组循环

[英]WSO2 EI For loop through Json array

I have a below request (i am using WSO2 Enterprise integrator 6.5.0): 我有以下要求(我正在使用WSO2 Enterprise integrator 6.5.0):

And I wanna get this 我想得到这个 in jsonpayload which is returned client 在返回客户端的jsonpayload中

<ERROR_RESP>
    <ERROR>
        <ECODE>ST-VALS-002</ECODE>
        <EDESC>Record Not Found  for Branch Code-CHO:Currency 1-USD:Currency 2-MN</EDESC>
    </ERROR>
    <ERROR>
        <ECODE>ST-SAVE-024</ECODE>
        <EDESC>Failed to Query Data</EDESC>
    </ERROR>
</ERROR_RESP>

It is my tried code to achieve it : 这是我尝试的代码来实现它:

<foreach expression="json-eval($.ERROR_RESP.ERROR)">
    <sequence>
        <payloadFactory media-type="json">
            <format>
            { 
                "ErrorCode" : "$1", 
                "ErrorMessage" : "$2"  
            }
                </format>
            <args>
                <arg evaluator="json" expression="$.ECODE"/>
                <arg evaluator="json" expression="$.EDESC"/>
            </args>
        </payloadFactory>
        <log level="full">
            <property name="MESSAGE" value="ENDLOOP"/>
        </log>
        <loopback/>
    </sequence>
</foreach> 
enter code here

I did some search but nothings worked, I think foreach expression is not right. 我进行了一些搜索,但没有任何效果,我认为foreach表达式不正确。

Thanks 谢谢

Regards, 问候,

In your sample the Loopback mediator is used. 在您的示例中,使用了环回调解器。 It will be used to move the message to the out flow (response path). 它将用于将消息移至流出(响应路径)。 Therefore, the Foreach mediator splits the message and sends the first message to the response path and end the flow. 因此,Foreach介体将消息拆分,然后将第一条消息发送到响应路径并结束流程。

You can prepare the XML payload and convert it to JSON using the messageType property with axis2 scope as follows. 您可以使用具有axis2范围的messageType属性来准备XML有效负载并将其转换为JSON,如下所示。

    <foreach expression="//ERROR">
        <sequence>
           <payloadFactory media-type="xml">
              <format>
                 <ERROR xmlns="">
                    <ErrorCode>$1</ErrorCode>
                    <ErrorMessage>$2</ErrorMessage>
                 </ERROR>
              </format>
              <args>
                 <arg evaluator="xml" expression="//ECODE/text()"/>
                 <arg evaluator="xml" expression="//EDESC/text()"/>
              </args>
           </payloadFactory>
        </sequence>
     </foreach>
     <property name="messageType" scope="axis2" value="application/json"/>

The final message will be like this: 最后的消息将是这样的:

{
    "ERROR_RESP": {
        "ERROR": [
            {
                "ErrorCode": "ST-VALS-002",
                "ErrorMessage": "Record Not Found  for Branch Code-CHO:Currency 1-USD:Currency 2-MN"
            },
            {
                "ErrorCode": "ST-SAVE-024",
                "ErrorMessage": "Failed to Query Data"
            }
        ]
    }
}

At the moment, foreach mediator in EI 6.5.0 does not support "json-eval()" expressions. 目前,EI 6.5.0中的foreach介体不支持“ json-eval()”表达式。 This feature will be included in the upcoming versions. 功能将包含在即将发布的版本中。

As a workaround, you can use XPath inside the expression. 解决方法是,可以在表达式内使用XPath。 You can use this blog as an example. 您可以使用此博客作为示例。 https://medium.com/@Manuri/wso2-esb-foreach-mediator-example-87f041e2a912 https://medium.com/@Manuri/wso2-esb-foreach-mediator-example-87f041e2a912

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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