3

I am using groovy's RESTClient to make an HTTP GET Request and have a JSON response object that looks like this

{   
    "page": 1,
    "pages": 1,
    "count": 4,
    "items": [{
        "id": 291938,
        "type": email,
        "folder": "1234",
        "isDraft": "false",
        "number": 349,
        "owner": {
            "id": 1234,
            "firstName": "Jack",
            "lastName": "Sprout",
            "email": "[email protected]",
            "phone": null,
            "type": "user"
        },
        "mailbox": {
            "id": 1234,
            "name": "My Mailbox"
      }
    }]
}

I set my code like so

def Client = new RESTClient('https://api.myapi.net/v1/conversations/' )
helpscout.setHeaders([Authorization: "Basic 1234", Accept: 'application/json'])
def resp = Client.get(contentType: JSON)
def myResponseObject = resp.getData()

I need to be able to loop through multiple API calls/JSON objects, and store all of the response items into their own variables/classes, and output them as necessary.

I'm coming from a python background, and am used to being able to call a specific item by saying responseObject['items']['id'], but in this case I can't do that.

My question to you is, how am I able to access one of these items, and do whatever I need to do with it/store it into a variable

This is my debugger output below enter image description here

3
  • Are you sure the sample json you provided is the same as what the endpoint is returning? Commented Aug 10, 2016 at 19:07
  • Bah! updated the actual JSON Output in the debugger It now prints out integers Commented Aug 10, 2016 at 19:22
  • It would be much easier to give some help if you would provide the actual JSON you get from the endpoint as text rather that providing a screenshot of debugger. According to the screenshot the JSON is totally different Commented Aug 10, 2016 at 19:52

1 Answer 1

2

As getData() returns a Map you can just reference the fields in the JSON like so:

...
def myResponseObject = resp.getData()
def page = myResponseObject.page
def count = myResponseObject.count
println page // prints 1
println count //prints 4

// Print all items
myResponseObject.items.each {
    println "item: $it"
}

// The python example responseObject['items']['id'] in groovy is
responseObject.items[0].id

// To get the first items createdAt field (according to the image)
println responseObject.items[0].createdAt
Sign up to request clarification or add additional context in comments.

7 Comments

I tried it and page returns null, see the screenshot that I added to the question
According to the image the myResponseObject has one entry called item, try it like this: myResponseObject.item.page
that gets items stores into a variable, then what do you do to call a specific item or field?
I'm not sure what you mean calling a specific item, could you give an example what you want to do with the fields?
Like how would I reference myResponseObject.items[1][4] ? It should give an output of 2016-08-10T19:17:30Z
|

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.