Forum Discussion

subhaD's avatar
subhaD
Contributor
6 years ago
Solved

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

  • 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

  • avidCoder's avatar
    avidCoder
    Super Contributor

    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 .