【问题标题】:Extracting child node from JSON Response using SoapUI-Groovy script使用 SoapUI-Groovy 脚本从 JSON 响应中提取子节点
【发布时间】:2025-12-08 13:15:01
【问题描述】:

我是 soapUI 的新手,还在探索 groovy 脚本,我需要你的帮助。 这是我的 json 回复:

 {
  "userId": 36,
  "userTypeId": 2,
  "name": "Name_7",
  "surname": "R",
  "friendlyName": "",
  "employeeId": "1000245",
  "role": "engineer",
  "dateOfBirth": "1985-07-09T01:46:23.213",
  "fPicture": "",
  "deleted": false,
  "visitingUserId": null,
  "mineId": 1,
  "crewId": null,
  "versionAutoId": 10002814,
  "externalId": null,
  "email": null,
  "externalLoginId": null,
  "sourceJson": null,
  "description": null,
  "propertyJson": null,
  "userGroups":       [
     {"groupCode": "ALL_USERS"},
     {"groupCode": "MTCC_ADMIN"}
  ],
  "userAccessDto":       {
     "userAccessId": 36,
     "userId": 36,
     "password": "XKjgXD6o/pjHaHd6swvkB8TiQ6L1kEC8307sV94F2GeiFnb4QXUZJhk8rdQJgvdJujSPK/NoM94CMtp8X51ExTEwMDAyNDU=",
     "deleted": false
  }
   },

{
  "userId": 37,
  "userTypeId": 5,
  "name": "Name_9",
  "surname": "R",
  "friendlyName": "",
  "employeeId": "1201",
  "role": "engineer",
  "dateOfBirth": "1985-07-09T01:46:23.213",
  "fPicture": "",
  "deleted": false,
  "visitingUserId": null,
  "mineId": 6,
  "crewId": null,
  "versionAutoId": 10031438,
  "externalId": null,
  "email": null,
  "externalLoginId": null,
  "sourceJson": null,
  "description": null,
  "propertyJson": null,
  "userGroups":       [
     {"groupCode": "ALL_USERS"},
     {"groupCode": "MTCC_ADMIN"}
  ],
  "userAccessDto":       {
     "userAccessId": 37,
     "userId": 37,
     "password": "a05qHK+KrXXmHTFFGQN9JRQWkHnjJX+SCmqBK1PAa2f95I8e20JNt5GaVxL5nGbnTReobSZ/vej3qCAsZK9Q7DEyMDE=",
     "deleted": false
  }

} ]

如何获取 userId = 36 的密码值并将其传递给我的其他测试用例?我进行了很多搜索,但找不到有关现有问题的适当指南。我应该提一下,我的响应会不时发生变化,所以我不能假设它是字符串。 感谢您的所有回复。

【问题讨论】:

  • 使用有效的 json 文件更新您的问题并添加您尝试过的内容..!

标签: groovy soapui


【解决方案1】:

我建议你使用JsonSlurper

对于这样的 Json 文件:Example.json

{
  "userAccessDto": {
    "userAccessId": 37,
    "userId": 37,
    "password": "a05qHK+KrXXmHTFFGQN9JRQWkHnjJX+SCmqBK1PAa2f95I8e20JNt5GaVxL5nGbnTReobSZ/vej3qCAsZK9Q7DEyMDE=",
    "deleted": "false"
  }
}

Groovy 代码:

import groovy.json.JsonSlurper;

def root =new JsonSlurper().parse(new File ('/tmp/example.json'))

println root.userAccessDto.password

【讨论】:

  • 感谢您的回复,是的,我使用 JsonSlurper 进行了尝试,并且成功了。我会附上回复。感谢您的帮助
  • @Ronak 很高兴!享受编码......!
【解决方案2】:
import net.sf.json.groovy.*

def i, newUserId, hashPass, empId;
//get test case from other project or from the same one
project = 
testRunner.getTestCase().getTestSuite().getProject().
getWorkspace().getProjectByName("API_Services_v3.0.321")
testSuite = project.getTestSuiteByName("Users");
testCase = testSuite.getTestCaseByName("Users-Retrieve a list of users");

// 运行测试用例

runner = testCase.run(new 
com.eviware.soapui.support.types.StringToObjectMap(), false);
Thread.sleep(3000)

//从testStep获取JSONresponse并解析

def responseContent = testCase.getTestStepByName("ApiV1UsersGet -Get 
all").getPropertyValue("response")
slurperResponse = new JsonSlurper().parseText(responseContent)

//从 TestSuite 属性中获取 userId 进行比较

newUserId = 
testRunner.testCase.testSuite.project.getTestSuiteByName("Users").
getPropertyValue("idOfUser")

//遍历 JSON 响应

for(i=0;i<slurperResponse.resource.size();i++)
{
            if(slurperResponse[i].userId == newUserId.toInteger() )
            {
                            log.info("Hash Pass is" +slurperResponse[i].userAccessDto.password);
                            log.info("Employee Id is" +slurperResponse[i].employeeId);
                            hashPass = slurperResponse[i].userAccessDto.password
                            empId = slurperResponse[i].employeeId

                            break;
            }
}

//将 hashPassword 和 empId 分配给 testSuite 属性以进行下一个测试步骤

testRunner.testCase.testSuite.project.getTestSuiteByName("Users")
.setPropertyValue("Hash_Pass",hashPass.toString())
 testRunner.testCase.testSuite.project.getTestSuiteByName("Users").
setPropertyValue("EmployeeId2",empId.toString())

【讨论】:

    【解决方案3】:

    我看到你找到了一种方法,尽管我认为它是困难的方法。您可以在 Property Transfer 中简单地使用 JSONPath 选项。 像这样的查询有效:

    $.[?(@.userId==36)].password

    此外,您可以将 userId 转换为用于动态检查的参数。

    【讨论】:

    • 哦哇!我肯定会尝试一下,是的,我需要的是 userId 的动态值。正如我所说,它会在每次测试运行中发生变化。有什么方法可以从我的 TestSuite 属性中获取 userId 值?我有 userId 属性,它的值是从 POST API 传输的。我如何指向 $.[?(@.userId==36)].password 来获取值让我从我的财产中说“36”?
    • @Ronak,我尝试过,但似乎不可能在 Groovy 中使用 JSONPath 而不是 slurper 仍然有一种简单的方法:community.smartbear.com/t5/SoapUI-Pro/…