Jmeter JSON 响应转换为带有请求修改的数组
Jmeter JSON response converting to an Array with request modification
你好,我想模拟这个场景,例如,如果我在下面得到这个响应,我想在我的后续请求中使用它。 appid 将用于 key 和 Role 到 value 范围。非常感谢您的回复。非常感谢。
要求:
[{
"appid": "4a157c66-4965-30b2-af47-7dc68651c350",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.235",
"Role" : "AD SERVER"},
{
"appid": "ba3fe4b4-1307-38e4-ace4-1636f8ffef3e",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.236",
"Role" : "TESTING SERVER"
}, {
"appid": "29b59fbb-ea93-3adf-ba9a-fd353abaac21",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.237",
"Role": "UAT SERVER"
}]
响应截图:
预期结果:
这里是 Groovy 的快速解决方案。
String response = """
[{
"appid": "4a157c66-4965-30b2-af47-7dc68651c350",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.235",
"Role" : "AD SERVER"},
{
"appid": "ba3fe4b4-1307-38e4-ace4-1636f8ffef3e",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.236",
"Role" : "TESTING SERVER"
}, {
"appid": "29b59fbb-ea93-3adf-ba9a-fd353abaac21",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.237",
"Role": "UAT SERVER"
}]
"""
String request = response.replace("appid", "key")
.replace("Role", "value")
.replaceAll("\"orgId\":\s?\".*\",\n", "")
.replaceAll("\"ipAddress\":\s?\".*\",\n", "")
你可以使用 JSON JMSEPath extractor to extract values and transform the keys. JMESPath Tutorial
[].{Key:Role,Value:orgId}
下面演示在查看结果树中测试 JSON JMESPath Query
。
这可能是另一种解决方案。
应该有更好的方法来替换键
import groovy.json.JsonGenerator
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
String response = """
[{
"appid": "4a157c66-4965-30b2-af47-7dc68651c350",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.235",
"Role" : "AD SERVER"},
{
"appid": "ba3fe4b4-1307-38e4-ace4-1636f8ffef3e",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.236",
"Role" : "TESTING SERVER"
}, {
"appid": "29b59fbb-ea93-3adf-ba9a-fd353abaac21",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.237",
"Role": "UAT SERVER"
}]
"""
def jsonSlurper = new JsonSlurper()
def responseJSON = jsonSlurper.parseText(response)
def generator = new JsonGenerator.Options()
.excludeNulls()
.excludeFieldsByName('orgId', 'ipAddress')
.build()
String request = generator.toJson(responseJSON)
request =JsonOutput.prettyPrint(request)
request = request.replace("appid", "key")
.replace("Role","value")
println(request)
你好,我想模拟这个场景,例如,如果我在下面得到这个响应,我想在我的后续请求中使用它。 appid 将用于 key 和 Role 到 value 范围。非常感谢您的回复。非常感谢。
要求:
[{
"appid": "4a157c66-4965-30b2-af47-7dc68651c350",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.235",
"Role" : "AD SERVER"},
{
"appid": "ba3fe4b4-1307-38e4-ace4-1636f8ffef3e",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.236",
"Role" : "TESTING SERVER"
}, {
"appid": "29b59fbb-ea93-3adf-ba9a-fd353abaac21",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.237",
"Role": "UAT SERVER"
}]
响应截图:
预期结果:
这里是 Groovy 的快速解决方案。
String response = """
[{
"appid": "4a157c66-4965-30b2-af47-7dc68651c350",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.235",
"Role" : "AD SERVER"},
{
"appid": "ba3fe4b4-1307-38e4-ace4-1636f8ffef3e",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.236",
"Role" : "TESTING SERVER"
}, {
"appid": "29b59fbb-ea93-3adf-ba9a-fd353abaac21",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.237",
"Role": "UAT SERVER"
}]
"""
String request = response.replace("appid", "key")
.replace("Role", "value")
.replaceAll("\"orgId\":\s?\".*\",\n", "")
.replaceAll("\"ipAddress\":\s?\".*\",\n", "")
你可以使用 JSON JMSEPath extractor to extract values and transform the keys. JMESPath Tutorial
[].{Key:Role,Value:orgId}
下面演示在查看结果树中测试 JSON JMESPath Query
。
这可能是另一种解决方案。 应该有更好的方法来替换键
import groovy.json.JsonGenerator
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
String response = """
[{
"appid": "4a157c66-4965-30b2-af47-7dc68651c350",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.235",
"Role" : "AD SERVER"},
{
"appid": "ba3fe4b4-1307-38e4-ace4-1636f8ffef3e",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.236",
"Role" : "TESTING SERVER"
}, {
"appid": "29b59fbb-ea93-3adf-ba9a-fd353abaac21",
"orgId": "205d64ff-9b58-3aad-bf48-29ec41dd4ef0",
"ipAddress": "192.168.100.237",
"Role": "UAT SERVER"
}]
"""
def jsonSlurper = new JsonSlurper()
def responseJSON = jsonSlurper.parseText(response)
def generator = new JsonGenerator.Options()
.excludeNulls()
.excludeFieldsByName('orgId', 'ipAddress')
.build()
String request = generator.toJson(responseJSON)
request =JsonOutput.prettyPrint(request)
request = request.replace("appid", "key")
.replace("Role","value")
println(request)