deadpool
deadpool

Reputation: 81

iterating through JSON objects in groovy using maps

    import com.avoka.component.http.GetRequest
    import groovy.json.JsonOutput
    import groovy.json.JsonSlurper
    import org.apache.http.client.methods.CloseableHttpResponse
    import org.apache.http.client.methods.HttpGet
    import org.apache.http.client.utils.URIBuilder
    import org.apache.http.impl.client.CloseableHttpClient
    import org.apache.http.impl.client.HttpClients
    import org.apache.http.util.EntityUtils


    CloseableHttpClient client = HttpClients.createDefault();
    def uri = new URIBuilder("https://randomuser.me/api/?results=30&nat=US")
    HttpGet request = new HttpGet(uri.build())
    request.setHeader("content-type", "application/json")
    CloseableHttpResponse response = client.execute(request);
    String json = EntityUtils.toString(response.getEntity());
    def jsonSlurper = new JsonSlurper()
    def object = jsonSlurper.parseText(json)
    def users =[:]
    for (int i =0 ; i< object.results.size() ; i++){
        def contactJson = object.results[i]

        users.gender = contactJson.gender
       users.firstname =contactJson.name.first
        users.lastname =contactJson.name.last
        users.location = contactJson.location.collect { it ->

            [Street  : it.street,
             city  : it.city,
             state  : it.state,
             postcode : it.postcode]

        }
        users.phone =contactJson.phone
        users.dateofbirth = contactJson.dob.age
        users.nationality =contactJson.nat
    }
    print users

I am looping the json object and trying to populate the response using maps. 
 Caught: groovy.lang.MissingPropertyException: No such property: street for class: java.util.LinkedHashMap$Entry

groovy.lang.MissingPropertyException: No such property: street for class: java.util.LinkedHashMap$Entry at post$_run_closure1.doCall(post.groovy:33) at post.run(post.groovy:31)

Getting this error and also i am getting a single user in the print user but the list has the size of 30.

Upvotes: 0

Views: 2926

Answers (1)

Opal
Opal

Reputation: 84786

  1. There's no need to collect over location field - this is not an instance of a collection but a single Map
  2. There's no need to use all these apache stuff.
  3. You want a list of maps where users is just a map in which you override keys over and over again with every iteration.

It all can be that simple:

import groovy.json.JsonSlurper

def slurped = new JsonSlurper().parse(new URL("https://randomuser.me/api/?results=30&nat=US"))

slurped
    .results
    .collect { u ->
        [
            gender: u.gender,
            firstname: u.name.first,
            lastname: u.name.last,
            location:[
                 street: u.location.street,
                 city: u.location.city,
                 state: u.location.state,
                 postcode: u.location.postcode
            ]                 
        ]
    }

Upvotes: 1

Related Questions