如何在 WSO2 ESB 中连接 JSON 数组值? [英] How to concatenate JSON array values in WSO2 ESB?

查看:21
本文介绍了如何在 WSO2 ESB 中连接 JSON 数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有如下的 json 对象,预期结果为:(001),(011),(089),(120)".

We have json object as like below, Expected Result in: "(001),(011),(089),(120)".

任何人都可以建议如何迭代 json 数组并将值连接为提及 ."(001),(011),(089),(120)"

Can anyone suggest how to iterate the json array and concat the values as mention ."(001),(011),(089),(120)"

提前致谢.

{
    "Element": {
        "Values": {
            "AgentID": "aaaaa",
            "TransactionData": [
                {
                    "No": "001"
                },
                {
                    "No": "011"
                },
                {
                    "No": "089"
                },
                {
                    "No": "120"
                }
            ]
        }
    }
}

推荐答案

您可以通过使用迭代介体、过滤介体和具有操作范围的属性来实现.试试这个解决方案.最后,您将在 concat-data 属性中获得 (001),(011),(089),(120) 值.我已经添加了完整的代理供您参考.

You can do it by using iterate mediator, filter mediator and properties with operation scope. Try this solution. At the end you will have (001),(011),(089),(120) value in concat-data property. I have added the complete proxy for your reference.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="StockQuoteProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <payloadFactory media-type="json">
            <format>
{
    "Element": {
        "Values": {
            "AgentID": "aaaaa",
            "TransactionData": [
                {
                    "No": "001"
                },
                {
                    "No": "011"
                },
                {
                    "No": "089"
                },
                {
                    "No": "120"
                }
            ]
        }
    }
}
</format>
            <args/>
         </payloadFactory>
         <iterate continueParent="true"
                  expression="//Element/Values/TransactionData"
                  sequential="true">
            <target>
               <sequence>
                  <property name="data"
                            expression="json-eval($.TransactionData.No)"
                            type="STRING"/>
                  <filter source="boolean(get-property('operation','concat-data'))" regex="false">
                     <then>
                        <property name="concat-data"
                                  expression="fn:concat('(',get-property('data'),')')"
                                  scope="operation"
                                  type="STRING"/>
                     </then>
                     <else>
                        <property name="concat-data"
                                  expression="fn:concat(get-property('operation','concat-data'),',','(',get-property('data'),')')"
                                  scope="operation"
                                  type="STRING"/>
                     </else>
                  </filter>
               </sequence>
            </target>
         </iterate>
         <log level="custom">
            <property name="con-cat-data"
                      expression="get-property('operation','concat-data')"/>
         </log>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <publishWSDL uri="file:repository/samples/resources/proxy/sample_proxy_1.wsdl"/>
   <description/>
</proxy>

Payload factory mediator 仅用于模拟您的场景.如果您的客户端发送此 JSON 有效负载,则您不需要拥有此有效负载工厂中介器.

Payload factory mediator is used only to simulate your scenario. If your client sends this JSON payload, then you don't need to have this payload factory mediator.

过滤介体用于省略前导逗号字符.如果您不使用过滤器,您将得到 ,(001),(011),(089),(120) 结果(注意前导逗号字符).当然,还有其他方法可以删除前导逗号字符.

Filter mediator is used to omit the leading comma character. If you do not use a filter, you will get ,(001),(011),(089),(120) as a result (note the leading comma character). Of course, there can be other ways to remove leading comma character.

参考这个有关具有操作范围的属性的更多详细信息.

Refer this for more details on properties with operation scope.

这篇关于如何在 WSO2 ESB 中连接 JSON 数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆