cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy If loop example

SOLVED
Occasional Contributor

Groovy If loop example

I am new to groovy scripting, am trying to loop through the rest response in below format

"addressData" : [
            {
               "type" : "MAILING",
                 "address" : {
                  "addressLines" : [
                     "",
                     "315 ST W"
                  ],
                  "city" : "",
                  "province" : "",
                  }
            },
            {
               "type" : "RESIDENTIAL",
               "verified" : false,
               "address" : {
                  "addressLines" : [
                     "",
                     ""
                  ],
                  "city" : "",
                  "province" : "",               
               }

 

I want to loop through and find the residential address and make sure has right value in every rest tag of that particular array, went through bideos but in time crunch.. hence wanna quick answer

 

ALSO, any appropriate course or tutorial on Groovy for READY API will really help

1 ACCEPTED SOLUTION

Accepted Solutions
Community Leader

Re: Groovy If loop example

Provided JSON is not correct. Here is the basic example how you can traverse through JSON:-

import groovy.json.JsonSlurper
def json = '''{"books":{"science":"biology", "commerce": "accountancy"}}'''

jSlurper = new JsonSlurper().parseText(json)
log.info (jSlurper.books.commerce);

This way if you have arrays of JSON, in that case you should use Using Gson . 

3 REPLIES 3
Community Manager

Re: Groovy If loop example

Hi SubhaD,

 

Please refer to the Groovy Script article describing basic features.

Is this what you are looking for?

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Community Leader

Re: Groovy If loop example

Provided JSON is not correct. Here is the basic example how you can traverse through JSON:-

import groovy.json.JsonSlurper
def json = '''{"books":{"science":"biology", "commerce": "accountancy"}}'''

jSlurper = new JsonSlurper().parseText(json)
log.info (jSlurper.books.commerce);

This way if you have arrays of JSON, in that case you should use Using Gson . 

Community Manager

Re: Groovy If loop example

Hi @subhaD,

 

Did the suggestions given here answer your question?

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Here?
Join us and watch the welcome video:
Top Kudoed Authors