Forum Discussion

Ashwin05's avatar
Ashwin05
Occasional Contributor
8 years ago

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

  • nmrao's avatar
    nmrao
    Champion Level 3
    What are you trying to achieve as just printing does help any way?
    • Ashwin05's avatar
      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.

      • nmrao's avatar
        nmrao
        Champion Level 3
        If values gets varying, then what specific values do you need to compare?
  • New2API's avatar
    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
    }