用于在响应中获取总数记录的JSON路径

时间:2015-09-11 08:44:22

标签: json soap wso2 wso2esb jsonpath

我正在使用wso2esb 4.9.0和Wso2dss3.0.0

我有来自DSS服务的SOAP响应,如下所示

 <soapenv:Header/>
   <soapenv:Body>
      <DataCollection xmlns="http://ws.wso2.org/dataservice">
         <Datalist>
            <partybranchid>2127629166052452</partybranchid>
            <partybranchname>other locations</partybranchname>
            <status>1</status>
            <partybranchtype>-1</partybranchtype>
            <partyid>460077453499443</partyid>
            <partyname>Yash Technologies Pvt Ltd</partyname>
            <ouid>-1</ouid>
            <organizationunitname>NONE</organizationunitname>
            <inchargeid>-1899999899</inchargeid>
            <sbuheadid>-1</sbuheadid>
            <usercriteriaconfigid>1187416899913503</usercriteriaconfigid>
            <primarymail>vikash@myunisoft.com</primarymail>
         </Datalist>
         <Datalist>
            <partybranchid>2545910581364239</partybranchid>
            <partybranchname>DBPB001</partybranchname>
            <status>1</status>
            <partybranchtype>2299733851146138</partybranchtype>
            <partyid>2534954639984412</partyid>
            <partyname>DBPRTY001</partyname>
            <ouid>2538210579514587</ouid>
            <organizationunitname>DBORG001</organizationunitname>
            <inchargeid>2540093122446574</inchargeid>
            <sbuheadid>1209449655666077</sbuheadid>
            <usercriteriaconfigid>1187416899913503</usercriteriaconfigid>
            <primarymail>vikash@myunisoft.com</primarymail>
         </Datalist>
      </DataCollection>
   </soapenv:Body>

从上面的SOAP响应中,我想获得Datalist记录到我的服务中的总数。为此,我给出了一个JSON属性文档,如下所示:

<property name="Total"
             expression="json-eval($.DataCollection.Datalist.length)"
             scope="default"
             type="STRING"/>

Total应该将值设为2,因为它们是两条记录,如您所见;但这是返回null。我认为我给出的JSON路径不起作用。

计算记录数的正确JSON路径是什么?

1 个答案:

答案 0 :(得分:1)

实际上,您似乎从DSS收到了XML文档。这就是为什么json函数不适用于xml内容的原因。我设法使用脚本调解器

来计算元素数量
<script language="js">var dl = mc.getPayloadXML()..*::Datalist;
   var cnt = dl.length();
   mc.setProperty("cnt", cnt);
</script>
<log level="full">
    <property name="cnt" expression="get-property('cnt')"/>
</log>

结果是身体上下文中的get属性,名称为&#34; cnt &#34;和价值&#34; 2 &#34;

假设您将u xml消息转换为json并获取此json有效负载(完全有效)

{"DataCollection": [
    {"Datalist": {
            "partybranchid": 2127629166052452,
            "partybranchname": "other locations",
            "status": 1,
            "partybranchtype": -1,
            "partyid": 460077453499443,
            "partyname": "Yash Technologies Pvt Ltd",
            "ouid": -1,
            "organizationunitname": "NONE",
            "inchargeid": -1899999899,
            "sbuheadid": -1,
            "usercriteriaconfigid": 1187416899913503,
            "primarymail": "vikash@myunisoft.com"
        }
    },
    {"Datalist": {
            "partybranchid": 2545910581364239,
            "partybranchname": "DBPB001",
            "status": 1,
            "partybranchtype": 2299733851146138,
            "partyid": 2534954639984412,
            "partyname": "DBPRTY001",
            "ouid": 2538210579514587,
            "organizationunitname": "DBORG001",
            "inchargeid": 2540093122446574,
            "sbuheadid": 1209449655666077,
            "usercriteriaconfigid": 1187416899913503,
            "primarymail": "vikash@myunisoft.com"
        }
    }
]
}

然后是JSONPATH表达式

$.DataCollection.length

返回数组的值长度,在这种情况下等于&#34; 2&#34;

有用的链接

或以json格式提供u有效载荷。