Gatling中JMESPath筛选失效:为何返回空序列?
问题:Gatling中JMESPath筛选totalChildren>0元素返回空序列
接口返回数据
假设GET /api/v1/elements接口返回如下JSON:
[ { "id": 1, "name": "Foo", "totalChildren": 0 }, { "id": 2, "name": "Bar", "totalChildren": 4 }, { "id": 3, "name": "Lala", "totalChildren": 0 } ]
需求
仅获取totalChildren > 0的元素
可行的JSONPath写法
以下JSONPath代码可正常筛选目标元素:
.exec(http("Get List GT 0") .get("/api/v1/elements") .headers(authorizationHeader) .asJson .check(jsonPath("$[?(@.totalChildren > 0)]") .ofType[Map[String, Any]] .findAll .saveAs("elements")))
无效的JMESPath写法(返回空Seq)
尝试用JMESPath替代时,以下写法返回空序列:
.exec(http("Get List GT 0") .get("/api/v1/elements") .headers(authorizationHeader) .asJson .check(jmesPath("[?totalChildren > '0']") .ofType[Seq[Any]] .saveAs("elements")))
问题原因及修正方案
问题出在JMESPath的条件表达式里:你把数字0用单引号包裹成了字符串'0',但接口返回的totalChildren是数字类型,字符串和数字的比较结果不匹配,导致没有符合条件的元素被筛选出来。
修正后的JMESPath写法:
.exec(http("Get List GT 0") .get("/api/v1/elements") .headers(authorizationHeader) .asJson .check(jmesPath("[?totalChildren > 0]") .ofType[Seq[Any]] .saveAs("elements")))
相关版本信息:
- gatlingVersion: 3.13.5
- scalaVersion: 2.13.16
内容的提问来源于stack exchange,提问作者Camilo Silva
相关产品推荐
相关产品推荐

