Forum Discussion

armygrad's avatar
armygrad
Contributor
3 months ago

Boolean check

Hi All

I'm almost there, i need to check that a json response contains 1234 within an array, and return true.

and if it doesn't return false.

boolean check = "FAIL"
if (response.contains("1234") ) {
	check = "PASS";
}

13 Replies

  • Hello armygrad 

    Is this a json or groovy question?  Your first line defining the 'check' variable starts the whole thing off on a bad foot...  Unless you are meaning to do some tricky groovyish stuff, i apologize...

    I don't know what groovy does when you mix data types...  line one you are setting a boolean type to a string type...  If you purposely add the boolean type why set to string value?

    You might get more consistent results with exacting code:

    .
    .
    .
    check = false;
    if (response.contains("1234") ) {
    	check = true;
    };
    log.info "check=$check";
    .
    .
    .

    Regards,

    Todd

    • ChrisAdams's avatar
      ChrisAdams
      Champion Level 3

      Hi,

      The result in quotes was part of a solution from another Q last week.  The Groovy check returns a String, which in turn is persisted in a Datasink step.  This part of the solution was not to assert as fail stops the test.  Instead, this part was to report all passes and fails.

  • Json response

    Fri Feb 23 15:30:16 GMT 2024: INFO: {"Area":{"2024-02-26":{"6395":[{"appt":1234,"Type":"ANY","Date":"26T08:30:00","Time":"08:30","DisplayDate":"26 

     

     

    • nmrao's avatar
      nmrao
      Champion Level 3

      Please find the sample code / groovy documentation here to read the json.

      If you still have issue, provide the valid json (no need to have the data, but structure is important so that correct path can be suggested.

  • Json

    I need to access Field 1

    {
       "Content" : {
          "2024-02-26" : {
             "2222" : [
                {
                   "Field1" : 101985,
                   "Field2" : "TYRE",
                   "Field3" : "2024-02-26T08:30:00",
                   "Field4" : "08:30",
                   "Field5" : "26 Feb 2024",
                   "Field6" : 1,
                   "Field7" : 60,
                   "Field8" : 6380,
                   "Field9" : 127,
                   "Field10" : "Past",
                   "Field11" : false,
                   "Field12" : 0,
                   "Field13" : null,
                   "Field14" : 2961655,
                   "Field15" : null,
                   "Field16" : null,
                   "Field17" : null
               }
             ]
          }
       }
    }
    • nmrao's avatar
      nmrao
      Champion Level 3

      Here it goes:

      def jsonString = '''{
         "Content" : {
            "2024-02-26" : {
               "2222" : [
                  {
                     "Field1" : 101985,
                     "Field2" : "TYRE",
                     "Field3" : "2024-02-26T08:30:00",
                     "Field4" : "08:30",
                     "Field5" : "26 Feb 2024",
                     "Field6" : 1,
                     "Field7" : 60,
                     "Field8" : 6380,
                     "Field9" : 127,
                     "Field10" : "Past",
                     "Field11" : false,
                     "Field12" : 0,
                     "Field13" : null,
                     "Field14" : 2961655,
                     "Field15" : null,
                     "Field16" : null,
                     "Field17" : null
                 }
               ]
            }
         }
      }'''
      def json = new groovy.json.JsonSlurper().parseText(jsonString) 
      assert json.Content.'2024-02-26'.'2222'[0].Field1 == 101985

       

       

  • def jsonString = '''{
       "Content" : {
          "2024-02-26" : {
             "2222" : [
                {
                   "Field1" : 101985,
                   "Field2" : "TYRE",
                   "Field3" : "2024-02-26T08:30:00",
                   "Field4" : "08:30",
                   "Field5" : "26 Feb 2024",
                   "Field6" : 1,
                   "Field7" : 60,
                   "Field8" : 6380,
                   "Field9" : 127,
                   "Field10" : "Past",
                   "Field11" : false,
                   "Field12" : 0,
                   "Field13" : null,
                   "Field14" : 2961655,
                   "Field15" : null,
                   "Field16" : null,
                   "Field17" : null
               }
             ]
          }
       }
    }'''
    def json = new groovy.json.JsonSlurper().parseText(jsonString) 
    assert json.Content.'2024-02-26'.'2222'[0].Field1 == 101985

    I swear that this is my last question on this, also i apologise for my stupidity.

    What i need to do is compare the value of Field 1 to what is in Datasourcebook1, then if both are the same then return true, but if DatasourceBook1 is different return false.

    it may be that i have to put 101985 into a variable then compare the 2 variables.

    what i have.

    def json = new groovy.json.JsonSlurper().parseText(jsonString) 
    assert json.Content.'2024-02-27'.'6380'[0].BranchCode == 101985
    
    check = false;
    if (json.contains == (datasourceBook1) ) {
    	check = true;
    };
    log.info "check=$check";
    • nmrao's avatar
      nmrao
      Champion Level 3

       

      What i need to do is compare the value of Field 1 to what is in Datasourcebook1, then if both are the same then return true, but if DatasourceBook1 is different return false.

      It is difficult to imagine the use case you are working on and answer as desired unless it is mentioned in the question.

      I am sure you will be able to retrieve the value from the source you mentioned and compare it instead of static value(which i gave because it was mentioned in the response.

      • ChrisAdams's avatar
        ChrisAdams
        Champion Level 3

        Hi,

        The use case is from a previous question... 

        https://community.smartbear.com/discussions/readyapi-questions/assertions/262648/Assertions | SmartBear Community

        In ArmyGrads Groovy snippet, the assertion isn't required for the use case.  But I can see why Rao added it for his demo to show that the value can be found.

        For ArmyGrads example to work, you need use Get Data to pull in the required value from the datasource step and assign it the var datasourceBook1 prior to the IF statement.

        Something like...

        def datasourceBook1 = context.expand( '${CustomerDataSource#Book1}' );

        Then you should both the response value and datasource values. 

         

  • nmrao's avatar
    nmrao
    Champion Level 3

    Thanks ChrisAdams for your inputs and followup from other questions of armygrad 

    If that is the case, and context.expand( '${CustomerDataSource#Book1}' )  can get the value, then just assertion needs to be change from the previous response and no need of If condition as assert will make the test fail automatically.

    assert json.Content.'2024-02-27'.'6380'[0].BranchCode ==   context.expand( '${CustomerDataSource#Book1}' )

    By the way, it appears the some of the keys seems to be dynamic such as date and some number, so need to be careful.

  • Thanks to all for the help, i have a solution that works. if anybody wants to see it i will gladly post it.

    Again many thanks

     

    ArmyGrad