Assertion returns null when json node has value
Hi everyone!
I am trying to get and assert the vale of the node first from the following json response but the assertion returns null even when the value is returned :
{"registers":{
"id": "123",
"date": "2018-08-01",
"name": {
"first": "George",
"last": "Smith"
}
}
}
This is my script assertion:
import groovy.json.JsonSlurper
def response=messageExchange.response.responseContent
def json=new JsonSlurper().parseText(response)
if (json.registers.name){
def first=json.registers.name.first
assert first!=null
}
The assertion fails as the json.registers.name.first returns null. But this shouldn't happen as there is a value for first in the response.
When I try log.info json.registers.name I get {first=George, last=Smith}. So why do I get null when I try to get the value of the first?
All help and ideas are appreciated.
Hi!
Thank you for answering so fast! But what I posted was not the real response but only a mock so that I could explain what the problem was (As I cannot publish online the real response) so that didn't work for me.
Today I tried something else and it worked. Here is I used in my script:
import groovy.json.JsonSlurper
def response=messageExchange.response.responseContent
def json=new JsonSlurper().parseText(response)if (json.registers.name){
def first=context.expand('${testStepName#Response#$[\'registers\'][\'name\'][\'first\']}')
assert first!=null
}
I can't figure out why the first script didn't work and returned null but this solution is working for me and I get the right value from the response.