0

I'm new to Groovy. But, I can try with some simple code lines to extract the data.

I have read the from Parse JSON using groovy script (using JsonSlurper). But it didn't help in my case.

My code line:

{
  "errors": false,
  "address_data": [
    {
      "address_id": "567",
      "township": {
        "id": "41079",
        "name": "Test Data"
      },
      "city": {
        "id": "1622",
        "name": "Test City"
      },
      "region": {
        "id": "663",
        "name": "Metro Test"
      },
      "stock_source_code": "Test",
      "is_default_address": false
    },
    {
      "address_id": "45444",
      "township": {
        "id": "41079",
        "name": "Test Test"
      },
      "city": {
        "id": "1622",
        "name": "Test City"
      },
      "region": {
        "id": "663",
        "name": "Metro Test Taguig"
      },
      "is_default_address": true
    },
    {
      "address_id": "45444",
      "township": {
        "id": "888888",
        "name": "Apas"
      },
      "city": {
        "id": "432",
        "name": "Test City"
      },
      "region": {
        "id": "591",
        "name": "Test Cebu"
      },
      "stock_source_code": "testce",
      "is_default_address": false
    }
  ]
}

My code lines:

def response = "data_above";
def object = new JsonSlurper().parseText(response);
def errors = object.errors
if (errors == false) { //could access object.errors property
    log.info "Checking condition"
    def addressData = object.address_data // Cannot get the data
}

How can I get the address_data property?

9
  • Try if (errors) - error is unboxed and == is unlikely to be the correct operation Commented Oct 4, 2019 at 7:07
  • thanks @ernest_k, it also works with == operation. My log message worked. Commented Oct 4, 2019 at 7:12
  • My bad. I forgot groovy == uses .equals... Commented Oct 4, 2019 at 7:13
  • 1
    What do you mean by Cannot get the data? Is addressData being set to null or are you getting an error? Commented Oct 4, 2019 at 7:15
  • I used println (object.address_data). But I didn't see any info. Commented Oct 4, 2019 at 7:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.