Forum Discussion

TejasParmar's avatar
TejasParmar
Occasional Contributor
7 years ago

How to get size of a list

Hi All,

 

I am new to groovy scripts. I have a JSON response as mentioned below. From here i want to extract the count of say values in "colour"(this value will differ in every iteration). So basically i want to check the size of each category.

This is the script that i wrote but it doesnt work

 jsonResponseMessage1 = testRunner.testCase.testSteps["preferenceCategory-IncludeOptions"].testRequest.response.contentAsString

def rootNode = new JsonSlurper().parseText jsonResponseMessage1

def expectedsize=rootNode._embedded.categories.options.size()

 

I am expecting a value like 22 for allergen, 4 for colour, etc

 

{
"_embedded" : {
"categories" : [
{
"name" : "Allergen",
"value" : "Allergen",
"options" : [
{
"name" : "Antibiotic allergy",
"value" : "Antibiotic allergy"
},
{
"name" : "Antihypertensive allergy",
"value" : "Antihypertensive allergy"
},
{
"name" : "Aspirin allergy",
"value" : "Aspirin allergy"
},
{
"name" : "Corn",
"value" : "Corn"
},
{
"name" : "Egg",
"value" : "Egg"
},
{
"name" : "Eggs",
"value" : "Eggs"
},
{
"name" : "Fish",
"value" : "Fish"
},
{
"name" : "Gluten",
"value" : "Gluten"
},
{
"name" : "Ibuprofen allergy",
"value" : "Ibuprofen allergy"
},
{
"name" : "Insulin allergy",
"value" : "Insulin allergy"
},
{
"name" : "Kosher",
"value" : "Kosher"
},
{
"name" : "Milk",
"value" : "Milk"
},
{
"name" : "Nonsteroidal allergy",
"value" : "Nonsteroidal allergy"
},
{
"name" : "Others(Specify)",
"value" : "Others(Specify)"
},
{
"name" : "Peanut",
"value" : "Peanut"
},
{
"name" : "Penicillin allergy",
"value" : "Penicillin allergy"
},
{
"name" : "ShellFish",
"value" : "ShellFish"
},
{
"name" : "Soy",
"value" : "Soy"
},
{
"name" : "Tree Nut",
"value" : "Tree Nut"
},
{
"name" : "Tree Nuts",
"value" : "Tree Nuts"
},
{
"name" : "Vegan",
"value" : "Vegan"
},
{
"name" : "Vegetarian",
"value" : "Vegetarian"
},
{
"name" : "Wheat",
"value" : "Wheat"
}
]
},
{
"name" : "Colour",
"value" : "Colour",
"options" : [
{
"name" : "Blue",
"value" : "Blue"
},
{
"name" : "Green",
"value" : "Green"
},
{
"name" : "Others(Specify)",
"value" : "Others(Specify)"
},
{
"name" : "Red",
"value" : "Red"
}
]
},
{
"name" : "Drinks",
"value" : "Drinks",
"options" : [
{
"name" : "Absinthe",
"value" : "Absinthe"
},
{
"name" : "Apple",
"value" : "Apple"
},
{
"name" : "Ballantines",
"value" : "Ballantines"
},
{
"name" : "Bud light",
"value" : "Bud light"
},
{
"name" : "Carlsberg",
"value" : "Carlsberg"
},
{
"name" : "Chardonnay",
"value" : "Chardonnay"
},
{
"name" : "Corona",
"value" : "Corona"
},
{
"name" : "Gin & Tonic",
"value" : "Gin & Tonic"
},
{
"name" : "Glenfiddich",
"value" : "Glenfiddich"
},
{
"name" : "Heineken",
"value" : "Heineken"
},
{
"name" : "Johnnie Walker",
"value" : "Johnnie Walker"
},
{
"name" : "Lemon Drop Martini",
"value" : "Lemon Drop Martini"
},
{
"name" : "McCallan",
"value" : "McCallan"
},
{
"name" : "Miller",
"value" : "Miller"
},
{
"name" : "Mint Julep",
"value" : "Mint Julep"
},
{
"name" : "Mixed Fruit",
"value" : "Mixed Fruit"
},
{
"name" : "Mojito",
"value" : "Mojito"
},
{
"name" : "Old Fashioned",
"value" : "Old Fashioned"
},
{
"name" : "Orange",
"value" : "Orange"
},
{
"name" : "Others(Specify)",
"value" : "Others(Specify)"
},
{
"name" : "Red Wine",
"value" : "Red Wine"
},
{
"name" : "Strawberry Daiquiri",
"value" : "Strawberry Daiquiri"
},
{
"name" : "Tequila",
"value" : "Tequila"
},
{
"name" : "White Wine",
"value" : "White Wine"
}
]
},
{
"name" : "Food",
"value" : "Food",
"options" : [
{
"name" : "Caviar",
"value" : "Caviar"
},
{
"name" : "Chim Chum",
"value" : "Chim Chum"
},
{
"name" : "Chowmein",
"value" : "Chowmein"
},
{
"name" : "Fajitas",
"value" : "Fajitas"
},
{
"name" : "Garlic Prawn Risotto",
"value" : "Garlic Prawn Risotto"
},
{
"name" : "Indian Cury",
"value" : "Indian Cury"
},
{
"name" : "Noodle Soup",
"value" : "Noodle Soup"
},
{
"name" : "Others(Specify)",
"value" : "Others(Specify)"
},
{
"name" : "Pasta",
"value" : "Pasta"
},
{
"name" : "Pizza",
"value" : "Pizza"
},
{
"name" : "Seafood",
"value" : "Seafood"
},
{
"name" : "Suki",
"value" : "Suki"
},
{
"name" : "Tortillas",
"value" : "Tortillas"
},
{
"name" : "Vegetable Au Gratin",
"value" : "Vegetable Au Gratin"
},
{
"name" : "Yam Chin Kai",
"value" : "Yam Chin Kai"
}
]
},
{
"name" : "Leisure",
"value" : "Leisure",
"options" : [
{
"name" : "Cinema",
"value" : "Cinema"
},
{
"name" : "Cosplay",
"value" : "Cosplay"
},
{
"name" : "Exercise",
"value" : "Exercise"
},
{
"name" : "Fishing",
"value" : "Fishing"
},
{
"name" : "Luxury Vacation Style",
"value" : "Luxury Vacation Style"
},
{
"name" : "Others(Specify)",
"value" : "Others(Specify)"
},
{
"name" : "Reading",
"value" : "Reading"
},
{
"name" : "Story Telling",
"value" : "Story Telling"
}
]
},
{
"name" : "Music",
"value" : "Music",
"options" : [
{
"name" : "Blues",
"value" : "Blues"
},
{
"name" : "Classic Rock",
"value" : "Classic Rock"
},
{
"name" : "Jazz",
"value" : "Jazz"
},
{
"name" : "Opera",
"value" : "Opera"
},
{
"name" : "Others(Specify)",
"value" : "Others(Specify)"
},
{
"name" : "Pop",
"value" : "Pop"
},
{
"name" : "Rock",
"value" : "Rock"
}
]
},
{
"name" : "Spa",
"value" : "Spa",
"options" : [
{
"name" : "Ayurvedic Spa",
"value" : "Ayurvedic Spa"
},
{
"name" : "Cruise Ship Spa",
"value" : "Cruise Ship Spa"
},
{
"name" : "Hot Tub",
"value" : "Hot Tub"
},
{
"name" : "Others(Specify)",
"value" : "Others(Specify)"
}
]
},
{
"name" : "Sports",
"value" : "Sports",
"options" : [
{
"name" : "Air Hockey",
"value" : "Air Hockey"
},
{
"name" : "Baseball",
"value" : "Baseball"
},
{
"name" : "BasketBall",
"value" : "BasketBall"
},
{
"name" : "Carrom",
"value" : "Carrom"
},
{
"name" : "Chess",
"value" : "Chess"
},
{
"name" : "Football",
"value" : "Football"
},
{
"name" : "Golf",
"value" : "Golf"
},
{
"name" : "Ice Polo",
"value" : "Ice Polo"
},
{
"name" : "Jet Skiing",
"value" : "Jet Skiing"
},
{
"name" : "Others(Specify)",
"value" : "Others(Specify)"
},
{
"name" : "Para-Sailing",
"value" : "Para-Sailing"
},
{
"name" : "Rock",
"value" : "Rock"
},
{
"name" : "Scuba-Diving",
"value" : "Scuba-Diving"
},
{
"name" : "Skurfing",
"value" : "Skurfing"
},
{
"name" : "Table Tennis",
"value" : "Table Tennis"
},
{
"name" : "VolleyBall",
"value" : "VolleyBall"
}
]
}
]
}
}

3 Replies

  • Radford's avatar
    Radford
    Super Contributor

    I've just taken another look at your question and hopefully the following code is a little clearer, I was a bit rushed previously.

     

    Note: It assumes that you have already defined the variable jsonData as per the above example.

     

    def rootNode = new JsonSlurper().parseText(jsonData)
    
    // Example of how to obtain a single known category
    
    def specificCategory = rootNode._embedded.categories.find{ it.name == 'Colour' }
    log.info('Category "' + specificCategory.name + '" has ' + specificCategory.options.size() + ' options.' )
    log.info('_____________')
    log.info(' ')
    
    // Example of how to loop through each category
    
    rootNode._embedded.categories.each() {category ->
    	log.info('The Category "' + category.name + '" has ' + category.options.size() + ' options.' )
    	category.options.each(){option ->
    		log.info('   - ' + option.value)
    	}
    }

     

  • Radford's avatar
    Radford
    Super Contributor

    Take a look at the following script it's a very rough and ready stand alone script that might help, it loops through each of the 8 "options" displaying the count of the name/value pairs:

     

    import groovy.json.JsonSlurper
    
    def jsonData = """{
    "_embedded" : {
    "categories" : [
    {
    "name" : "Allergen",
    "value" : "Allergen",
    "options" : [
    {
    "name" : "Antibiotic allergy",
    "value" : "Antibiotic allergy"
    },
    {
    "name" : "Antihypertensive allergy",
    "value" : "Antihypertensive allergy"
    },
    {
    "name" : "Aspirin allergy",
    "value" : "Aspirin allergy"
    },
    {
    "name" : "Corn",
    "value" : "Corn"
    },
    {
    "name" : "Egg",
    "value" : "Egg"
    },
    {
    "name" : "Eggs",
    "value" : "Eggs"
    },
    {
    "name" : "Fish",
    "value" : "Fish"
    },
    {
    "name" : "Gluten",
    "value" : "Gluten"
    },
    {
    "name" : "Ibuprofen allergy",
    "value" : "Ibuprofen allergy"
    },
    {
    "name" : "Insulin allergy",
    "value" : "Insulin allergy"
    },
    {
    "name" : "Kosher",
    "value" : "Kosher"
    },
    {
    "name" : "Milk",
    "value" : "Milk"
    },
    {
    "name" : "Nonsteroidal allergy",
    "value" : "Nonsteroidal allergy"
    },
    {
    "name" : "Others(Specify)",
    "value" : "Others(Specify)"
    },
    {
    "name" : "Peanut",
    "value" : "Peanut"
    },
    {
    "name" : "Penicillin allergy",
    "value" : "Penicillin allergy"
    },
    {
    "name" : "ShellFish",
    "value" : "ShellFish"
    },
    {
    "name" : "Soy",
    "value" : "Soy"
    },
    {
    "name" : "Tree Nut",
    "value" : "Tree Nut"
    },
    {
    "name" : "Tree Nuts",
    "value" : "Tree Nuts"
    },
    {
    "name" : "Vegan",
    "value" : "Vegan"
    },
    {
    "name" : "Vegetarian",
    "value" : "Vegetarian"
    },
    {
    "name" : "Wheat",
    "value" : "Wheat"
    }
    ]
    },
    {
    "name" : "Colour",
    "value" : "Colour",
    "options" : [
    {
    "name" : "Blue",
    "value" : "Blue"
    },
    {
    "name" : "Green",
    "value" : "Green"
    },
    {
    "name" : "Others(Specify)",
    "value" : "Others(Specify)"
    },
    {
    "name" : "Red",
    "value" : "Red"
    }
    ]
    },
    {
    "name" : "Drinks",
    "value" : "Drinks",
    "options" : [
    {
    "name" : "Absinthe",
    "value" : "Absinthe"
    },
    {
    "name" : "Apple",
    "value" : "Apple"
    },
    {
    "name" : "Ballantines",
    "value" : "Ballantines"
    },
    {
    "name" : "Bud light",
    "value" : "Bud light"
    },
    {
    "name" : "Carlsberg",
    "value" : "Carlsberg"
    },
    {
    "name" : "Chardonnay",
    "value" : "Chardonnay"
    },
    {
    "name" : "Corona",
    "value" : "Corona"
    },
    {
    "name" : "Gin & Tonic",
    "value" : "Gin & Tonic"
    },
    {
    "name" : "Glenfiddich",
    "value" : "Glenfiddich"
    },
    {
    "name" : "Heineken",
    "value" : "Heineken"
    },
    {
    "name" : "Johnnie Walker",
    "value" : "Johnnie Walker"
    },
    {
    "name" : "Lemon Drop Martini",
    "value" : "Lemon Drop Martini"
    },
    {
    "name" : "McCallan",
    "value" : "McCallan"
    },
    {
    "name" : "Miller",
    "value" : "Miller"
    },
    {
    "name" : "Mint Julep",
    "value" : "Mint Julep"
    },
    {
    "name" : "Mixed Fruit",
    "value" : "Mixed Fruit"
    },
    {
    "name" : "Mojito",
    "value" : "Mojito"
    },
    {
    "name" : "Old Fashioned",
    "value" : "Old Fashioned"
    },
    {
    "name" : "Orange",
    "value" : "Orange"
    },
    {
    "name" : "Others(Specify)",
    "value" : "Others(Specify)"
    },
    {
    "name" : "Red Wine",
    "value" : "Red Wine"
    },
    {
    "name" : "Strawberry Daiquiri",
    "value" : "Strawberry Daiquiri"
    },
    {
    "name" : "Tequila",
    "value" : "Tequila"
    },
    {
    "name" : "White Wine",
    "value" : "White Wine"
    }
    ]
    },
    {
    "name" : "Food",
    "value" : "Food",
    "options" : [
    {
    "name" : "Caviar",
    "value" : "Caviar"
    },
    {
    "name" : "Chim Chum",
    "value" : "Chim Chum"
    },
    {
    "name" : "Chowmein",
    "value" : "Chowmein"
    },
    {
    "name" : "Fajitas",
    "value" : "Fajitas"
    },
    {
    "name" : "Garlic Prawn Risotto",
    "value" : "Garlic Prawn Risotto"
    },
    {
    "name" : "Indian Cury",
    "value" : "Indian Cury"
    },
    {
    "name" : "Noodle Soup",
    "value" : "Noodle Soup"
    },
    {
    "name" : "Others(Specify)",
    "value" : "Others(Specify)"
    },
    {
    "name" : "Pasta",
    "value" : "Pasta"
    },
    {
    "name" : "Pizza",
    "value" : "Pizza"
    },
    {
    "name" : "Seafood",
    "value" : "Seafood"
    },
    {
    "name" : "Suki",
    "value" : "Suki"
    },
    {
    "name" : "Tortillas",
    "value" : "Tortillas"
    },
    {
    "name" : "Vegetable Au Gratin",
    "value" : "Vegetable Au Gratin"
    },
    {
    "name" : "Yam Chin Kai",
    "value" : "Yam Chin Kai"
    }
    ]
    },
    {
    "name" : "Leisure",
    "value" : "Leisure",
    "options" : [
    {
    "name" : "Cinema",
    "value" : "Cinema"
    },
    {
    "name" : "Cosplay",
    "value" : "Cosplay"
    },
    {
    "name" : "Exercise",
    "value" : "Exercise"
    },
    {
    "name" : "Fishing",
    "value" : "Fishing"
    },
    {
    "name" : "Luxury Vacation Style",
    "value" : "Luxury Vacation Style"
    },
    {
    "name" : "Others(Specify)",
    "value" : "Others(Specify)"
    },
    {
    "name" : "Reading",
    "value" : "Reading"
    },
    {
    "name" : "Story Telling",
    "value" : "Story Telling"
    }
    ]
    },
    {
    "name" : "Music",
    "value" : "Music",
    "options" : [
    {
    "name" : "Blues",
    "value" : "Blues"
    },
    {
    "name" : "Classic Rock",
    "value" : "Classic Rock"
    },
    {
    "name" : "Jazz",
    "value" : "Jazz"
    },
    {
    "name" : "Opera",
    "value" : "Opera"
    },
    {
    "name" : "Others(Specify)",
    "value" : "Others(Specify)"
    },
    {
    "name" : "Pop",
    "value" : "Pop"
    },
    {
    "name" : "Rock",
    "value" : "Rock"
    }
    ]
    },
    {
    "name" : "Spa",
    "value" : "Spa",
    "options" : [
    {
    "name" : "Ayurvedic Spa",
    "value" : "Ayurvedic Spa"
    },
    {
    "name" : "Cruise Ship Spa",
    "value" : "Cruise Ship Spa"
    },
    {
    "name" : "Hot Tub",
    "value" : "Hot Tub"
    },
    {
    "name" : "Others(Specify)",
    "value" : "Others(Specify)"
    }
    ]
    },
    {
    "name" : "Sports",
    "value" : "Sports",
    "options" : [
    {
    "name" : "Air Hockey",
    "value" : "Air Hockey"
    },
    {
    "name" : "Baseball",
    "value" : "Baseball"
    },
    {
    "name" : "BasketBall",
    "value" : "BasketBall"
    },
    {
    "name" : "Carrom",
    "value" : "Carrom"
    },
    {
    "name" : "Chess",
    "value" : "Chess"
    },
    {
    "name" : "Football",
    "value" : "Football"
    },
    {
    "name" : "Golf",
    "value" : "Golf"
    },
    {
    "name" : "Ice Polo",
    "value" : "Ice Polo"
    },
    {
    "name" : "Jet Skiing",
    "value" : "Jet Skiing"
    },
    {
    "name" : "Others(Specify)",
    "value" : "Others(Specify)"
    },
    {
    "name" : "Para-Sailing",
    "value" : "Para-Sailing"
    },
    {
    "name" : "Rock",
    "value" : "Rock"
    },
    {
    "name" : "Scuba-Diving",
    "value" : "Scuba-Diving"
    },
    {
    "name" : "Skurfing",
    "value" : "Skurfing"
    },
    {
    "name" : "Table Tennis",
    "value" : "Table Tennis"
    },
    {
    "name" : "VolleyBall",
    "value" : "VolleyBall"
    }
    ]
    }
    ]
    }
    }
    """
    
    def rootNode = new JsonSlurper().parseText(jsonData)
    def expectedsize=rootNode._embedded.categories.options.size()
    def countOfNameValuPairs = 0
    
    log.info('Count of "options" nodes = ' + expectedsize)
    	
    rootNode._embedded.categories.options.each() {option ->
    	log.info(option.size())
    	countOfNameValuPairs = countOfNameValuPairs + option.size()
    }
    
    log.info('Total count of name value pairs = ' + countOfNameValuePairs)