Forum Discussion

Vivi's avatar
Vivi
Occasional Contributor
6 years ago

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.

 

  • Vivi's avatar
    Vivi
    6 years ago

    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.

  • groovyguy's avatar
    groovyguy
    Community Hero

    I copied your response and your groovy script assertion, and I do not get a failure for first being null. When I run it, it passes fine. I even included a statement that would fail if first was anything OTHER than "George", and it ran without issue. 

     

    if (first != "George")
    	{
    		assert false;
    	}
    • Vivi's avatar
      Vivi
      Occasional Contributor

      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.