【问题标题】:Remove duplicates array elemets using Script mediator in wso2 esb使用 wso2 esb 中的脚本调解器删除重复的数组元素
【发布时间】:2021-06-23 23:00:48
【问题描述】:

我想使用 wso2 esb 脚本调解器删除重复项。我正在使用 WSO2 ESB 为需要返回转换后的 JSON 数组的指定端点创建代理服务。我已经在输出序列中设置了一个带有 foreach 调解器和 payloadfactory 的代理服务。结果是无法发布环境 生产和沙盒:在网关更新序列时出错。结果是只返回最后一个元素。

我有一个带有重复元素数组的 json 对象

原始端点返回如下内容:

{
  "colors": [
    "Red",
    "Blue",
    "Red",
    "Yellow",
    "White",
    "Yellow"
  ]
}

所需结果:

{
  "colors": [
    "Blue",
    "Red",
    "Yellow",
    "White"]
}

我的调解政策:

<sequence
    xmlns="http://ws.apache.org/ns/synapse"  name="legalCost">
    <!-- Used to extract the elements from the JSON payload and then assigns them to the variables $1 and $2 respectively. First argument is assigned to the variable $1 and the second to $2-->
    <payloadFactory media-type="json">
        <!-- Here you specify the format of the json message you need to output -->
        <format>
                            {
                              "products": $2
                            }
                        </format>
        <foreach expression="///jsonArray/jsonElement"" name="JSONPayload" evaluator="json">
            <!-- REturns all <foreach expression="$.policyItemSummary.[?(@.productLine)]" evaluator="json"/> -->
            <script language="js">
                <![CDATA[
                    var payload = mc.getProperty("JSONPayload");
                    results = payload.results;
                    response = new Array();
                    for (i = 0; i &lt; results.length; ++i) {
                        if (response.indexOf(payload[i]) === -1) {
                            response.push(payload[i]);
                        }
                       }
                    mc.setPayloadJSON(response);
                    ]]>
            </script>
        </foreach>
        <args>
            <arg evaluator="json" expression="get-property('response')"/>
        </args
    
    </payloadFactory>
    <property name="messageType" value="application/json" scope="axis2"/>
</sequence> 

【问题讨论】:

  • 使用这个:[...new Set(&lt;paste your array here&gt;)]

标签: javascript arrays json wso2 wso2esb


【解决方案1】:

让我们稍微修改一下提到的中介序列来满足您的要求。下面给出的是中介序列的摘录,用于读取响应有效负载并在过滤后将其设置回

<property name="JSONPayload" expression="json-eval($.)" />
<script language="js">
    <![CDATA[
        importPackage(Packages.java.util);
        var log = mc.getServiceLog();
        
        var response = mc.getProperty("JSONPayload");
        var jsonPayload = JSON.parse(response);
        var colorList = jsonPayload['colors'];

        filtered = new Array();
        for (i = 0; i < colorList.length; ++i) {
            if (filtered.indexOf(colorList[i]) === -1) {
                filtered.push(colorList[i]);
            }
        }

        // printing the elements
        // for (var i = 0; i < filtered.length; i++) {
        //    log.info(filtered[i]);
        // }

        jsonPayload['colors'] = filtered;
        // log.info(JSON.stringify(jsonPayload));

        mc.setPayloadJSON(jsonPayload);
    ]]>
</script>
<!-- continue on setting up the response in the payload factory -->

希望这可以帮助您实现您的要求

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-02
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多