fambo fambo - 2 years ago 175
Groovy Question

How to use variable to extract value from JSON response using Groovy?

I'm trying to extract bicycle brand "Cannondale" from the JSON response by using the location of the value stored in a variable called "jsonFieldName"

Alternatively, I'm able to successfully extract the brand value by using following syntax:

def brand = json.store.bicycle.brand

however, I want to keep the location of the element in a variable. Reason being, is that I want to be able to iterate multiple assertions on the Json Response as part of my automation suite.

Can someone kindly advise how to do this?

Below is the snippet I've currently got in place storing the location in a variable. But its not working and always returns brand as 'Null' :(

def response = ('''{
"store": {
"book": [
"title": "Sword of Honour",
"category": "fiction",
"author": "Evelyn Waugh",
"@price": 12.99
"title": "Moby Dick",
"category": "fiction",
"author": "Herman Melville",
"isbn": "0-553-21311-3",
"@price": 8.99
"title": "Sayings of the Century",
"category": "reference",
"author": "Nigel Rees",
"@price": 8.95
"title": "The Lord of the Rings",
"category": "fiction",
"author": "J. R. R. Tolkien",
"isbn": "0-395-19395-8",
"@price": 22.99
"bicycle": {
"brand": "Cannondale",
"color": "red",
"price": 19.95

//store location of json property I want to extract in property called jsonFieldName
def jsonFieldName = "store.bicycle.brand"
def json = new JsonSlurper().parseText (response)
//perform extraction
brand = json."${jsonFieldName}"

Answer Source

The problem is that using String to access a field, the string is considered the name of the field so you can not use to access more than one depth property; in other words the . is considered part of the property name. A possible work around could be to split your string by the . character and access the properties one by one:

 def jsonFieldName = "store.bicycle.brand"
 def json = new JsonSlurper().parseText (response)

   jsonFieldName.split("\\.").each{json = json[it]}

assert json == 'Cannondale'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download