Ask a Question

Unable to print list of all nodes including child nodes of a Json response using groovy script

Ashwin05
Occasional Contributor

Unable to print list of all nodes including child nodes of a Json response using groovy script

Unable to print all node values and also child node values of a JSON response using groovy script in a Soap UI basic version

 

Below is my response

{
   "lookupResponse": {"branches":    {
      "longitudeForMap": "-87.541405",
      "latitudeForMap": "37.976581",
      "foundWithinRadius": true,
      "branch": [      {
         "branchNumber": "123",
         "location": "Balt-NE, IN",
         "addressLine1": "test RD",
         "addressLine2": "",
         "city": "Baltimore",
         "state": "IN",
         "zip": "47715",
         "branchManager": "tester, DAN",
         "branchEmail": "test0435@gmail.com",
         "phoneNumber": "(123) 479 - 6895",
         "mloId": "98521",
         "longitude": "-87.492300",
         "latitude": "37.993700",
         "distance": "2.920000",
         "speaksSpanish": false,
         "faxNumber": "(123) 473 - 5349",
        
      }]
   }},
   "referenceNumber": "12345678",
   "status": "SUCCESS"
}

 

Below is my Groovy code:


import groovy.json.*


     
              def response = context.expand( '${'+strRequestName+'#Response}' )  
             def jsonResponse_Act = new JsonSlurper().parseText(response)
           log.info "node lesngth is "+ jsonResponse_Act.size()
  for(int nodeid=0 ; nodeid<int_nodelength;nodeid++)
              {

                                               
             //Getting error at this statement
                             log.info jsonResponse_Act[nodeid]
                            
               }

Tried this also

        def nodes=jsonResponse_Act.lookupResponse
        log.info "size "+ nodes.size()
         log.info "value  "+ nodes[0]
    

 

6 REPLIES 6
nmrao
Community Hero

What are you trying to achieve as just printing does help any way?


Regards,
Rao.
Ashwin05
Occasional Contributor

I wanted to capture all the actual node and compare it with expected response nodes. I cannot compare the whole response as most of the node values would change every time, so I would want to check if all the expected nodes are available or not. 

To achieve this I thought of comparing with list of actual nodes names and expected.

If values gets varying, then what specific values do you need to compare?


Regards,
Rao.
Ashwin05
Occasional Contributor

Hi Rao,

 

 

As per my requirement I need to capture all node values individually  & store into an Excel without passing the node names.

 

Apart from this I also need to compare few nodes like branch number, status and branch email etc to compared with expected.

 

 

 

Not sure why to store hierarchical data (json in your case) and put it in flat format? How do you accommodate json arrays? IMO, it does not make sense to put in excel.

 

In case it is needed for later use, just persist the entire response into a file and give file reference in the excel if it is mandatory so you would not lose context where the file and what was the response  then.

 

It is very much possible to compare the specific keys of the json.



Regards,
Rao.
New2API
Frequent Contributor

@Ashwin05, try below code. But if you need to further drill down into loopupResponse, branches or branch, you would need to change the script.

 

import groovy.json.*


def response = context.expand( '${'+strRequestName+'#Response}' )
def jsonResponse_Act = new JsonSlurper().parseText(response)
log.info "node length is "+ jsonResponse_Act.size()

jsonResponse_Act.each{
                                        log.info it
}

cancel
Showing results for 
Search instead for 
Did you mean: