Ask a Question

JSONPath Existence Path error - InvalidPatchException

davecoleman
Contributor

JSONPath Existence Path error - InvalidPatchException

hi all,

I have the following JSON response and want to validate that as well as the parent node value checks that the sub elements are also correct.

 

{
"parent" : [
{
"country" : "RO",
"statusCode" : "NotFound"
},
{
"country" : "UK",
"statusCode" : "Found",
"foundEntity" : {
"respondingArea" : "Authority",
"entityDetails" : {
"NameDetails" : {
"familyName" : "Smith",
"firstName" : "John"
}
}
}
}
]
}

 

When I create a JSONPath existence match Assertion for following:

 

$..[?(@.country=="UK" && @.statusCode=="Found")] && $..[?(@.familyName=="Smith")]

 

I get the below error.

davecoleman_2-1655102777215.png

"InvalidPathException:Could not parse token starting at position" (after the double ambersand)

Testing via Jsonpath.com shows this is a valid jsonPath string and result given correctly. 

davecoleman_0-1655102495782.png

 

Any alternatives to assert parent and child content that it exists otherwise in ReadyAPI?

thanks,

Dave

2 REPLIES 2
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.

 

 

further research into a solution brought me to: 

https://community.smartbear.com/t5/ReadyAPI-Questions/Groovy-Script-assertion-always-passes-even-tho...

 

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")

	}
}

 

 

cancel
Showing results for 
Search instead for 
Did you mean: