SoapUI Groovy-使用json slurper对象过滤器提取子节点

时间:2018-07-06 09:10:20

标签: groovy soapui jsonslurper

Json响应的摘录-

[
    {
      "id": "ada2ac5d-57f1-4624-b35a-4fd34ada9e56",
      "userId": "userid",
      "firstName": "firstname",
      "lastName": "lastname",
      "dateAdded": "2018-07-06T04:19:21.57",
      "dateOfChange": "2018-07-06T04:19:22.14",
      "lastAction": "Updated",
   },
      {
      "id": "2f395120-5440-4770-8a2e-16a77aa69c1e",
      "userId": "userid",
      "firstName": "firstname",
      "lastName": "lastname",
      "dateAdded": "2018-07-06T04:19:21.57",
      "dateOfChange": "2018-07-06T04:19:21.99",
      "lastAction": "Updated",
   },
      {
      "id": "7aaa6b11-0f43-414b-a999-b03114392c21",
      "userId": "userid",
      "firstName": "firstname",
      "lastName": "lastname",
      "dateAdded": "2018-07-06T04:19:21.693",
      "dateOfChange": "2018-07-06T04:19:21.743",
      "lastAction": "Added",
   },
]

=====================

我想基于ID匹配捕获“ lastAction”值。

我尝试了以下操作-

responseContent = testStepp.getPropertyValue("response") //getting response
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(responseContent)

log.info "last action : : "

object.('$.[?(@.id=="ada2ac5d-57f1-4624-b35a-4fd34ada9e56")].lastAction')

结果-

最后一个动作:: [null,null,null,null,null,null,null,null,null,null]

2 个答案:

答案 0 :(得分:1)

尝试使用Groovy的.find()函数:

object.find { it.id == 'ada2ac5d-57f1-4624-b35a-4fd34ada9e56' }.lastAction

答案 1 :(得分:1)

object.find{it.id=="7aaa6b11-0f43-414b-a999-b03114392c21"}?.lastAction