Forum Discussion

Bharadwaj's avatar
Bharadwaj
Contributor
7 years ago
Solved

Parsing a simple SOAPUI JSON Response in Groovy

Hi Guys,

I am new to Soap UI and Groovy. I am looking to develop a generic function in Groovy which takes a SOAPUI rest response which is a JSON ,access all the objects in it and print it on to the console,something like the code below, I have searched google but could not get much help ,I found the solution as described below, but it really did not help my cause

Solution found on slack overflow

 

Requirement 1 :  A generic groovy function to print any rest API JSON response as key value pairs on to the console.

 

Please Note : I have no idea of field names inside the JSON response before hand,so the example I gave below won't work in my case.

 

Requirement 2 : Read the Rest API JSON response into a groovy collection(Map if possible or some kind of generic collection) and again iterate through that collection and access it as key value pairs.

 

 

static void PrintJSONResponse(String jsonResponse)

{

    //parse the response

    def map  = new groovy.json.JsonSlurper().parse(new File('Test.json'))

   //iterate through the response and print the key value pairs in the response
    map.each { key, value ->
  println "$key : $value" //this code generates error for the json file I have attached
} //the above code generates errors as below,so how to achieve this?
}
Wed Feb 14 10:47:08 AEDT 2018:ERROR:An error occurred [No signature of method: Script41$_run_closure1.doCall() is applicable for argument types: (groovy.json.internal.LazyMap) values: [[bar:true, foo:42]]
Possible solutions: doCall(java.lang.Object, java.lang.Object), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)], see error log for details

 

 

I have executed this code and got the result but the issue is I want a generic function,as I have no idea what the contents(field names inside the response) of the response json are. So my requirement is no matter what ever json response I pass it the function should parse all the objects and print them as key ,value pairs (please fins the sample json file attached)

 

 

 

def map  = new groovy.json.JsonSlurper().parse(new File('D:/Work/AQA/APIAutomation/DataFiles/Expected Data JSON Files/Test.json'))
map.each { 
  log.info "Name : "+it.name
  log.info "ID : " + it.uid
}

Wed Feb 14 11:26:49 AEDT 2018:INFO:Name : Bob
Wed Feb 14 11:26:49 AEDT 2018:INFO:ID : 10512213
Wed Feb 14 11:26:49 AEDT 2018:INFO:Name : John
Wed Feb 14 11:26:49 AEDT 2018:INFO:ID : 7208201
Wed Feb 14 11:26:49 AEDT 2018:INFO:Name : Jim
Wed Feb 14 11:26:49 AEDT 2018:INFO:ID : 10570
Wed Feb 14 11:26:49 AEDT 2018:INFO:Name : Sally
Wed Feb 14 11:26:49 AEDT 2018:INFO:ID : 1799657

 

 

  • The structure of your json data with nesting is different to the flat example in slack overflow.

     

    If you don't expect that structure to change then try

     

     

    map.each {it.each 
        { key, value ->
        log.info "$key : $value"
        }
    }

     

     

2 Replies

  • PaulMS's avatar
    PaulMS
    Super Contributor

    The structure of your json data with nesting is different to the flat example in slack overflow.

     

    If you don't expect that structure to change then try

     

     

    map.each {it.each 
        { key, value ->
        log.info "$key : $value"
        }
    }