How to concatenate JSON array values in WSO2 ESB?

大憨熊 提交于 2020-01-05 14:04:15

问题


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

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

Thanks in advance.

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

回答1:


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

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.




回答2:


For this kind of logic I sometimes favor the script mediator, as IMO it is simpler to implement things like string splicing/joining, conditional xml/json element building and so forth.



来源:https://stackoverflow.com/questions/31694929/how-to-concatenate-json-array-values-in-wso2-esb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!