Forum Discussion
davecoleman
Contributor
if i changed the JSON response to:
{
"parent": [
{
"country": "RO",
"statusCode": "NotFound"
},
{
"country": "UK",
"statusCode": "Found",
"foundEntity": [ {
"respondingArea": "Authority",
"entityDetails": {
"NameDetails": {
"familyName": "Smith",
"firstName": "John"
},
"respondingArea": "Authority2",
"entityDetails": {
"NameDetails": {
"familyName": "Daniels",
"firstName": "Mike"
}
}
} }
]
}
]
}
i tried the following Script Assertion but no joy iterating through the fields to find the FamilyName based on it being within the "UK/Found" parent. comes back with null continuously 😞
import com.eviware.soapui.support.JsonPathFacade
def found = 0
def jpf = new JsonPathFacade(messageExchange.responseContent)
def testArray = jpf.readObjectValue("parent")
log.info (testArray)
testArray.each {
def country= it.get("country")
def statusCode = it.get("statusCode")
def familyName = it.get("foundEntity.entityDetails.NameDetails.familyName)
log.info ("country: " +country)
log.info ("FamilyName: " +familyName)
if(country.contains("UK") && statusCode.contains("Found") && familyName.contains("Smith")) {
log.info("matching record: $country")
found++
}
}
assert found > 0: "*****Assertion of data combination. did not find***"
o the child elements.
davecoleman
3 years agoContributor
further research into a solution brought me to:
Working through the below Groovy but its giving me a NullPointerException on cannot get property 'familyName' on null object error at line 52 which is this value: slurpedResponse[i].foundEntity.entityDetails.NameDetails[j].familyName
Assertion works but why the null exception? and why null data returned for this array in the 2nd FOR loop?
import groovy.json.*
import groovy.util.*
def jsonText = '''
{
"parent": [
{
"country": "RO",
"statusCode": "NotFound"
},
{
"country": "UK",
"statusCode": "Found",
"foundEntity": [ {
"respondingArea": "Authority",
"entityDetails": {
"NameDetails": {
"familyName": "Smith",
"firstName": "John"
},
"respondingArea": "Authority2",
"entityDetails": {
"NameDetails": {
"familyName": "Daniels",
"firstName": "Mike"
}
}
} }
]
}
]
}
'''
//remove the parent level and parse to the actual content
def slurpedResponse = new JsonSlurper().parseText(jsonText).parent
log.info (slurpedResponse)
def count = slurpedResponse.Size
for(i=0; i< count ; i++) {
log.info ("Current country: " +slurpedResponse[i].country)
if(slurpedResponse[i].country=="UK"){
log.info(" Current status Code: " +slurpedResponse[i].statusCode)
if(slurpedResponse[i].statusCode == "Found") {
def countsub = slurpedResponse.foundEntity.Size
log.info ("Count of sub element: " +countsub)
for (j=0; j<countsub; j++){
log.info (j)
log.info (slurpedResponse[i].foundEntity.entityDetails.NameDetails[j])
log.info (" familyName: " +slurpedResponse[i].foundEntity.entityDetails.NameDetails[j].familyName)
if (slurpedResponse[i].foundEntity.entityDetails.NameDetails[j].familyName=="Smith"){
log.info ("current familyName: " +slurpedResponse[i].foundEntity.entityDetails.NameDetails[j].familyName)
assert slurpedResponse[i].foundEntity.entityDetails.NameDetails[j].familyName == "Smith"
}
}
}
}
else {
log.info("The Name is not found")
}
}
Related Content
- 6 years ago