Forum Discussion
skelkar
11 years agoContributor
Hi,
I want to assert existance of a node using json slurper.
The code I found over net has info how to assert if node is NOT null or it has a specific value.
But What I am looking for is how to validate if node even exists or not.
I can do that using xml [holder.getDomNode(xPath) != null] but do not know how to do via json slurper.
slurper example : only indicates how to assert node not null and node value equals to expected but I am missing code to assert if node ever exists or not.
Response:
{
"Id": 1,
"Type": "String",
"GUID": "String-ABC"
}
//imports
import groovy.json.JsonSlurper
//grab the response
def ResponseMessage = messageExchange.response.responseContent
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
//verify the slurper isn't empty
assert !(jsonSlurper.isEmpty())
//verify the Id, Type and Guid aren't null
assert jsonSlurper.Id != null
//verify Id is 1
assert jsonSlurper.Id == 1
I want to assert existance of a node using json slurper.
The code I found over net has info how to assert if node is NOT null or it has a specific value.
But What I am looking for is how to validate if node even exists or not.
I can do that using xml [holder.getDomNode(xPath) != null] but do not know how to do via json slurper.
slurper example : only indicates how to assert node not null and node value equals to expected but I am missing code to assert if node ever exists or not.
Response:
{
"Id": 1,
"Type": "String",
"GUID": "String-ABC"
}
//imports
import groovy.json.JsonSlurper
//grab the response
def ResponseMessage = messageExchange.response.responseContent
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
//verify the slurper isn't empty
assert !(jsonSlurper.isEmpty())
//verify the Id, Type and Guid aren't null
assert jsonSlurper.Id != null
//verify Id is 1
assert jsonSlurper.Id == 1