【发布时间】: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 < 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(<paste your array here>)]
标签: javascript arrays json wso2 wso2esb