You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSO2 EI/MI中如何用XPath/JSONPath动态获取指定条件的JSON数组元素

WSO2 EI/MI中动态提取JSON数组指定字段值的解决方案

问题需求

从给定JSON payload中,不依赖固定数组下标,获取满足Loading/Discount Type值为Experience Based Discount的attributes数组对应的LD Rate值(示例结果为0.7)。

示例JSON payload

{"multisetAttribute": [{"attributes": [{"name": "Loading/Discount Type","value": "Bonus Discount"},{"name": "Premium of Cover","value": "9550"},{"name": "LD Rate","value": "0.35"},{"name": "LD Amount","value": "3343"}]},{"attributes": [{"name": "Loading/Discount Type","value": "Renewal Loading - OD"},{"name": "Premium of Cover","value": "1862"},{"name": "LD Rate","value": "0.0"},{"name": "LD Amount","value": "0"}]},{"attributes": [{"name": "Loading/Discount Type","value": "Experience Based Discount"},{"name": "Premium of Cover","value": "6208"},{"name": "LD Rate","value": "0.7"},{"name": "LD Amount","value": "4345"}]}],"name": "Loading Discount Details","value": null}

用户尝试的XPath表达式未得到正确结果,现提供可行的XPath、JSONPath表达式及完整实现方案。


解决方案

1. XPath表达式方案(基于WSO2默认JSON转XML格式)

WSO2会自动将JSON转换为XML结构,正确的XPath需先定位到包含目标Loading/Discount Type的multisetAttribute条目,再提取对应LD Rate的值:

//multisetAttribute[attributes[name='Loading/Discount Type' and value='Experience Based Discount']]/attributes[name='LD Rate']/value/text()

逻辑说明:

  • 先筛选出attributes数组中存在name='Loading/Discount Type'且value='Experience Based Discount'的multisetAttribute元素
  • 再从该元素的attributes数组中,提取name='LD Rate'对应的value值

2. JSONPath表达式方案(直接操作JSON)

使用WSO2的json evaluator直接解析JSON,兼容不同版本的表达式如下:

方式一(支持ES6箭头函数的环境)

$.multisetAttribute[?(@.attributes.some(item => item.name == 'Loading/Discount Type' && item.value == 'Experience Based Discount'))].attributes.find(item => item.name == 'LD Rate').value

方式二(通用兼容版)

$.multisetAttribute[?(@.attributes[?(@.name == 'Loading/Discount Type' && @.value == 'Experience Based Discount')])].attributes[?(@.name == 'LD Rate')].value

修改后的完整API代码

以下是使用XPath方案的API示例,同时修正了原payloadFactory的格式错误:

<?xml version="1.0" encoding="UTF-8"?>
<api context="/jsonarrayapi" name="jsonarrayapi"
    xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <!-- 使用正确的XPath提取LD Rate -->
            <property expression="//multisetAttribute[attributes[name='Loading/Discount Type' and value='Experience Based Discount']]/attributes[name='LD Rate']/value/text()" name="LD_Rate" scope="default" type="STRING"/>
            <log level="custom">
                <property expression="$ctx:LD_Rate" name="Extracted_LD_Rate"/>
            </log>
            <!-- 修正payloadFactory的格式错误 -->
            <payloadFactory media-type="json">
                <format>{"LD_Rate":"$1"}</format>
                <args>
                    <arg evaluator="xml" expression="$ctx:LD_Rate"/>
                </args>
            </payloadFactory>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

内容的提问来源于stack exchange,提问作者Justin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 05:10:04