Groovy:将响应与 json 文件进行比较,同时忽略某些键
Groovy: Compare a response with a json file while ignoring certain keys
我收到来自 REST API 约 40k 行的响应,我需要验证其中的值是否与现有本地文件中的值匹配。响应确实有一个特定的动态元素,它基于收到响应的日期和时间。
我需要将响应与本地 json 文件进行比较并确保值匹配,同时忽略通过的特定日期和时间元素。
这是 response/json 文件的片段示例:
[
{
"code" : "SJM",
"valuesCount" : [
{
"code" : "SJM",
"description" : "LAST_NAME FIRST_NAME-SJM (LMM/null)",
"period" : "November 2020",
"printedOn" : "31/01/2022 09:39:39",
"name" : "null",
"logo" : { },
"standardId" : true,
"dailyValues" : [
{
"index" : "odd",
"day" : "01",
"description" : "",
"time" : "23:59",
"cms" : "",
"hcv" : "",
"nm" : "",
"hcp" : "",
"hcz" : "",
"synDc" : "",
"jiJnm" : "",
"c1" : "",
"c2" : "",
"proHvs" : "",
"dcd" : "",
"dcs" : "",
"d1" : "",
"d2" : "",
"nbdays" : ""
},
]
}
}
]
]
该文件包含数百个 code
条目,每个条目都有自己的 valuesCount
映射,其中出现了 printedOn
元素,这就是我试图忽略的内容。
我一直在 Groovy 中尝试这样做,我在 groovy 文档中找到的唯一东西是 removeAll()
方法,我认为我可以删除完全 key:value 但我认为我没有正确使用它。
import groovy.json.*
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectDir = groovyUtils.projectPath
def response = new JsonSlurper().parseText(context.expand( '${Request#Response}' ))
File jsonFile = new File(projectDir, "/Project/Resources/jsonFile.json")
def defaultValues = new JsonSlurper().parse(jsonFile)
var1 = response.removeAll{"printedOn"}
var2 = defaultValues.removeAll{"printedOn"}
if (var1 == var2) {
log.info "The request matches the default values."
} else {
throw new Exception("The request does not match the default values.");
}
这只是 returns 两种情况的真实陈述。
谁能指出我正确的方向?
非常感谢。
您需要遍历 lists/maps 的层次结构以更改内部映射并删除 printedOn
key/value 对。
你的 json 也被 missing/extra 括号破坏了。
以下代码:
import groovy.json.*
def data1 = '''\
[
{
"code": "SJM",
"valuesCount": [
{
"code": "SJM",
"description": "LAST_NAME FIRST_NAME-SJM (LMM/null)",
"period": "November 2020",
"printedOn": "31/01/2022 09:39:39",
"name": "null",
"logo": {},
"standardId": true,
"dailyValues": [
{
"index": "odd",
"day": "01",
"description": "",
"time": "23:59",
"cms": "",
"hcv": "",
"nm": "",
"hcp": "",
"hcz": "",
"synDc": "",
"jiJnm": "",
"c1": "",
"c2": "",
"proHvs": "",
"dcd": "",
"dcs": "",
"d1": "",
"d2": "",
"nbdays": ""
}
]
}
]
}
]'''
def data2 = '''\
[
{
"code": "SJM",
"valuesCount": [
{
"code": "SJM",
"description": "LAST_NAME FIRST_NAME-SJM (LMM/null)",
"period": "November 2020",
"printedOn": "25/11/2021 09:39:39",
"name": "null",
"logo": {},
"standardId": true,
"dailyValues": [
{
"index": "odd",
"day": "01",
"description": "",
"time": "23:59",
"cms": "",
"hcv": "",
"nm": "",
"hcp": "",
"hcz": "",
"synDc": "",
"jiJnm": "",
"c1": "",
"c2": "",
"proHvs": "",
"dcd": "",
"dcs": "",
"d1": "",
"d2": "",
"nbdays": ""
}
]
}
]
}
]'''
def parser = new JsonSlurper()
def json1 = parser.parseText(data1)
def json2 = parser.parseText(data2)
def clean1 = removeNoise(json1)
def clean2 = removeNoise(json2)
def out1 = JsonOutput.toJson(clean1)
def out2 = JsonOutput.toJson(clean2)
assert out1 == out2
if(out1 == out2) println "the two documents are identical"
else println "the two documents differ"
def removeNoise(json) {
json.collect { codeMap ->
codeMap.valuesCount = codeMap.valuesCount.collect { valuesMap ->
valuesMap.remove('printedOn')
valuesMap
}
codeMap
}
}
执行时会产生如下输出:
─➤ groovy solution.groovy
the two documents are identical
─➤
上面代码中的 json 已更正为可解析。我手动更改了字符串日期,以便 data1 和 data2 在 printedOn
属性.
上有所不同
编辑 - 看来你实际上可以这样做:
assert clean1 == clean2
// or
if (clean1 == clean2) ...
用于比较。减少了 json 的额外序列化步骤,但也让我觉得在比较像这样的嵌套数据结构时相信 groovy 做正确的事情有点不舒服。
我可能还是会继续连载。
我收到来自 REST API 约 40k 行的响应,我需要验证其中的值是否与现有本地文件中的值匹配。响应确实有一个特定的动态元素,它基于收到响应的日期和时间。
我需要将响应与本地 json 文件进行比较并确保值匹配,同时忽略通过的特定日期和时间元素。
这是 response/json 文件的片段示例:
[
{
"code" : "SJM",
"valuesCount" : [
{
"code" : "SJM",
"description" : "LAST_NAME FIRST_NAME-SJM (LMM/null)",
"period" : "November 2020",
"printedOn" : "31/01/2022 09:39:39",
"name" : "null",
"logo" : { },
"standardId" : true,
"dailyValues" : [
{
"index" : "odd",
"day" : "01",
"description" : "",
"time" : "23:59",
"cms" : "",
"hcv" : "",
"nm" : "",
"hcp" : "",
"hcz" : "",
"synDc" : "",
"jiJnm" : "",
"c1" : "",
"c2" : "",
"proHvs" : "",
"dcd" : "",
"dcs" : "",
"d1" : "",
"d2" : "",
"nbdays" : ""
},
]
}
}
]
]
该文件包含数百个 code
条目,每个条目都有自己的 valuesCount
映射,其中出现了 printedOn
元素,这就是我试图忽略的内容。
我一直在 Groovy 中尝试这样做,我在 groovy 文档中找到的唯一东西是 removeAll()
方法,我认为我可以删除完全 key:value 但我认为我没有正确使用它。
import groovy.json.*
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectDir = groovyUtils.projectPath
def response = new JsonSlurper().parseText(context.expand( '${Request#Response}' ))
File jsonFile = new File(projectDir, "/Project/Resources/jsonFile.json")
def defaultValues = new JsonSlurper().parse(jsonFile)
var1 = response.removeAll{"printedOn"}
var2 = defaultValues.removeAll{"printedOn"}
if (var1 == var2) {
log.info "The request matches the default values."
} else {
throw new Exception("The request does not match the default values.");
}
这只是 returns 两种情况的真实陈述。
谁能指出我正确的方向? 非常感谢。
您需要遍历 lists/maps 的层次结构以更改内部映射并删除 printedOn
key/value 对。
你的 json 也被 missing/extra 括号破坏了。
以下代码:
import groovy.json.*
def data1 = '''\
[
{
"code": "SJM",
"valuesCount": [
{
"code": "SJM",
"description": "LAST_NAME FIRST_NAME-SJM (LMM/null)",
"period": "November 2020",
"printedOn": "31/01/2022 09:39:39",
"name": "null",
"logo": {},
"standardId": true,
"dailyValues": [
{
"index": "odd",
"day": "01",
"description": "",
"time": "23:59",
"cms": "",
"hcv": "",
"nm": "",
"hcp": "",
"hcz": "",
"synDc": "",
"jiJnm": "",
"c1": "",
"c2": "",
"proHvs": "",
"dcd": "",
"dcs": "",
"d1": "",
"d2": "",
"nbdays": ""
}
]
}
]
}
]'''
def data2 = '''\
[
{
"code": "SJM",
"valuesCount": [
{
"code": "SJM",
"description": "LAST_NAME FIRST_NAME-SJM (LMM/null)",
"period": "November 2020",
"printedOn": "25/11/2021 09:39:39",
"name": "null",
"logo": {},
"standardId": true,
"dailyValues": [
{
"index": "odd",
"day": "01",
"description": "",
"time": "23:59",
"cms": "",
"hcv": "",
"nm": "",
"hcp": "",
"hcz": "",
"synDc": "",
"jiJnm": "",
"c1": "",
"c2": "",
"proHvs": "",
"dcd": "",
"dcs": "",
"d1": "",
"d2": "",
"nbdays": ""
}
]
}
]
}
]'''
def parser = new JsonSlurper()
def json1 = parser.parseText(data1)
def json2 = parser.parseText(data2)
def clean1 = removeNoise(json1)
def clean2 = removeNoise(json2)
def out1 = JsonOutput.toJson(clean1)
def out2 = JsonOutput.toJson(clean2)
assert out1 == out2
if(out1 == out2) println "the two documents are identical"
else println "the two documents differ"
def removeNoise(json) {
json.collect { codeMap ->
codeMap.valuesCount = codeMap.valuesCount.collect { valuesMap ->
valuesMap.remove('printedOn')
valuesMap
}
codeMap
}
}
执行时会产生如下输出:
─➤ groovy solution.groovy
the two documents are identical
─➤
上面代码中的 json 已更正为可解析。我手动更改了字符串日期,以便 data1 和 data2 在 printedOn
属性.
编辑 - 看来你实际上可以这样做:
assert clean1 == clean2
// or
if (clean1 == clean2) ...
用于比较。减少了 json 的额外序列化步骤,但也让我觉得在比较像这样的嵌套数据结构时相信 groovy 做正确的事情有点不舒服。
我可能还是会继续连载。