Hello,
Could you please help me to understand how to check if JSON contains some value for any key
response:
[
{
"id":"76D12D26",
"name":"FM-10",
"machine_name":"sssb",
"history":[
{
"track":{
"id":"826B2557",
"name":"8"
},
"output":{
"wear":6,
"id":"5F4B9864",
},
"up_machine_name":"sssa"
},
{
"track":{
"id":"D5D38891",
"name":"8",
},
"output":{
"wear":1,
"id":"66CB401A"
},
"up_machine_name":"sssb"
}],
"refurbishments":[
{
"id":"AE019767",
"type":"Double"
}]
},
{
"id":"BFB38570",
"name":"FM-10.1",
"history":[
{
"track":{
"id":"1189D324",
"name":"6"
},
"output":{
"wear":6,
"id":"5F4B9864"
},
"up_machine_name":"sssb"
},
{
"track":{
"id":"623F14BA",
"name":"6"
},
"output":{
"wear":1,
"id":"66CB401A"
},
"extraordinary":true,
"up_machine_name":"sssf"
}],
"refurbishments":[
{
"id":"6C28A13B",
"refurbishment_event_id":"5809C9FE"
}]
}]
I try, but I can’t understand what is wrong with type of data. I try nested closures. The keys are checked only on the upper level.
JsonSlurper slurper = new JsonSlurper()
def m = slurper.parseText(response)
m.eachWithIndex { entry, i ->
println i
println "entry $entry"
println entry.size()
entrySize1 = entry.size()
def result3 = entry.findAll{entry.value == "sssf" || entry.value == "sssa" }
println " result3 $result3"
entry.eachWithIndex { entry_2 , i1 ->
println " $i1 INSIDE JSON"
println " $entry_2"
println entry_2.getClass()
println entry_2.getMetaClass()
def result4 = entry_2.findAll{entry_2.value == "sssf" || entry_2.value == "sssa" }
println " result4 $result4"
entry_2.eachWithIndex { entry_3 , i3 ->
println " $i3 INSIDE JSON 3"
println " $entry_3"
println entry_3.getClass()
println entry_3.getMetaClass()
def result5 = entry_3.findAll{entry_3.value == "sssf" || entry_3.value == "sssa" }
println " result5 $result5"
}
}
}
logs
2024-07-10 12:48:13.467 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2024-07-10 12:48:13.477 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/Tools/Web plus API/in work/test - Copy - Copy
2024-07-10 12:48:14.960 DEBUG testcase.test - Copy - Copy - 1: response = "[
{
"id":"76D12D26",
"name":"FM-10",
"machine_name":"sssb",
"history":[
{
"track":{
"id":"826B2557",
"name":"8"
},
"output":{
"wear":6,
"id":"5F4B9864",
},
"up_machine_name":"sssa"
},
{
"track":{
"id":"D5D38891",
"name":"8",
},
"output":{
"wear":1,
"id":"66CB401A"
},
"up_machine_name":"sssb"
}],
"refurbishments":[
{
"id":"AE019767",
"type":"Double"
}]
},
{
"id":"BFB38570",
"name":"FM-10.1",
"history":[
{
"track":{
"id":"1189D324",
"name":"6"
},
"output":{
"wear":6,
"id":"5F4B9864"
},
"up_machine_name":"sssb"
},
{
"track":{
"id":"623F14BA",
"name":"6"
},
"output":{
"wear":1,
"id":"66CB401A"
},
"extraordinary":true,
"up_machine_name":"sssf"
}],
"refurbishments":[
{
"id":"6C28A13B",
"refurbishment_event_id":"5809C9FE"
}]
}]"
2024-07-10 12:48:14.968 DEBUG testcase.test - Copy - Copy - 2: slurper = new groovy.json.JsonSlurper()
2024-07-10 12:48:15.026 DEBUG testcase.test - Copy - Copy - 3: m = slurper.parseText(response)
2024-07-10 12:48:15.067 DEBUG testcase.test - Copy - Copy - 4: map1 = "[{"disc_name":"FM-10.1"}]"
2024-07-10 12:48:15.086 DEBUG testcase.test - Copy - Copy - 5: m.eachWithIndex({ java.lang.Object entry, java.lang.Object i -> ... })
0
entry [id:76D12D26, name:FM-10, machine_name:sssb, history:[[track:[id:826B2557, name:8], output:[wear:6, id:5F4B9864], up_machine_name:sssa], [track:[id:D5D38891, name:8], output:[wear:1, id:66CB401A], up_machine_name:sssb]], refurbishments:[[id:AE019767, type:Double]]]
5
result3 [:]
0 INSIDE JSON
id=76D12D26
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result4 []
0 INSIDE JSON 3
id=76D12D26
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result5 []
1 INSIDE JSON
name=FM-10
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result4 []
0 INSIDE JSON 3
name=FM-10
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result5 []
2 INSIDE JSON
machine_name=sssb
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result4 []
0 INSIDE JSON 3
machine_name=sssb
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result5 []
3 INSIDE JSON
history=[{track={id=826B2557, name=8}, output={wear=6, id=5F4B9864}, up_machine_name=sssa}, {track={id=D5D38891, name=8}, output={wear=1, id=66CB401A}, up_machine_name=sssb}]
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result4 []
0 INSIDE JSON 3
history=[{track={id=826B2557, name=8}, output={wear=6, id=5F4B9864}, up_machine_name=sssa}, {track={id=D5D38891, name=8}, output={wear=1, id=66CB401A}, up_machine_name=sssb}]
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result5 []
4 INSIDE JSON
refurbishments=[{id=AE019767, type=Double}]
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result4 []
0 INSIDE JSON 3
refurbishments=[{id=AE019767, type=Double}]
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result5 []
1
entry [id:BFB38570, name:FM-10.1, history:[[track:[id:1189D324, name:6], output:[wear:6, id:5F4B9864], up_machine_name:sssb], [track:[id:623F14BA, name:6], output:[wear:1, id:66CB401A], extraordinary:true, up_machine_name:sssf]], refurbishments:[[id:6C28A13B, refurbishment_event_id:5809C9FE]]]
4
result3 [:]
0 INSIDE JSON
id=BFB38570
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result4 []
0 INSIDE JSON 3
id=BFB38570
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result5 []
1 INSIDE JSON
name=FM-10.1
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result4 []
0 INSIDE JSON 3
name=FM-10.1
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result5 []
2 INSIDE JSON
history=[{track={id=1189D324, name=6}, output={wear=6, id=5F4B9864}, up_machine_name=sssb}, {track={id=623F14BA, name=6}, output={wear=1, id=66CB401A}, extraordinary=true, up_machine_name=sssf}]
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result4 []
0 INSIDE JSON 3
history=[{track={id=1189D324, name=6}, output={wear=6, id=5F4B9864}, up_machine_name=sssb}, {track={id=623F14BA, name=6}, output={wear=1, id=66CB401A}, extraordinary=true, up_machine_name=sssf}]
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result5 []
3 INSIDE JSON
refurbishments=[{id=6C28A13B, refurbishment_event_id=5809C9FE}]
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result4 []
0 INSIDE JSON 3
refurbishments=[{id=6C28A13B, refurbishment_event_id=5809C9FE}]
class java.util.LinkedHashMap$Entry
org.codehaus.groovy.runtime.HandleMetaClass@32091c14[groovy.lang.MetaClassImpl@32091c14[class java.util.LinkedHashMap$Entry]]
result5 []
2024-07-10 12:48:15.444 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/Tools/Web plus API/in work/test - Copy - Copy
Thank you in advance for your answers.