简体   繁体   中英

How to find if a property name exists in json response using Groovy script

I am trying to use Groovy script for my Json response in soapui and finding if the property exists in my response. I used If statement as below. For what ever property name i use to check it returns only true statement even though if the property not exists. I am not sure whats wrong I am doing here.

import groovy.json.JsonSlurper

def slurper = new JsonSlurper()
def i = 0

responseContent = testRunner.testCase.getTestStepByName("DAY").getPropertyValue("response")
slurperresponse = new JsonSlurper().parseText(responseContent)

if(slurperresponse.day_details.activities.to_locans) {
     println "************************"
     res = "Crew_base found" 
} else {
     res = "Crew_base not found"
}

This is caused by a few things coming together:

  1. Groovy returns a null for unknown keys
  2. Groovy lets you use xpath type variable lookup, including if that goes via a nested list
  3. Groovy truthy on a list is based on the list size - even if the list only contains null, if its not empty, then it evaluates to true.

Your comment above reveals the problem:

For valid property name

Fri Apr 22 16:01:19 EDT 2016:INFO:<java.util.ArrayList@260080 elementData=[[NYP, NYP]] size=1 modCount=1> 

For invalid property name

Fri Apr 22 16:01:35 EDT 2016:INFO:<java.util.ArrayList@3e0 elementData=[[null, null]] size=1 modCount=1> 

You see in both cases the result is a list (of lists) of two items - so always evaluates to true.

As a map, from your final comment where you print the dump of that variable, it looks like this:

[
        day_details: [
            [
                fra_status:'', block_training_day:0, holiday_name:'', activities:[
                    [ from_location:'WAS', craft:'Conductor', departure_datetime:'2016-04-26T13:02:00-04:00', end_datetime:'2016-04-26T16:30:00-04:00', crew_base:'NYP', ends_next_day:false, description:'Unit', train_number:172, to_location:'NYP', arrival_datetime:'2016-04-26T16:30:00-04:00', zone:'Zone 2', start_datetime:'2016-04-26T12:52:00-04:00' ], 
                    [ from_location:'NYP', craft:'Engineer', to_location:'WAS', zone:'Zone 2', start_datetime:'2016-04-26T17:55:00-04:00' ]
                ]
            ]
        ]
    ]

Reading that, you will see that day_details and activities are both nested lists (hence the two lists in the response - so groovy navigates to each of the maps inside activities and looks for the key to_locans - not finding the key, it returns null, and puts those two nulls into a list.

Changing the if check to the following will work as you originally intended (that is with the assumption that you only want to ever consider the first element in each of those lists):

if (slurperresponse.day_details[0].activities[0].to_locans){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM