JSON in Groovy / Grails

17,971

Solution 1

There are a few problems with your sample code. First of all, to access GET and JSON that way, you need to statically import them:

import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.JSON

That will make the code compile, but not run successfully. Your url.path value needs a leading '/' (as shown on the HTTPBuilder page). More importantly, the JSON that comes back from the URL you're referencing has a totally different structure from that returned by the example code that performs a Google search. If you load your URL into the very handy JSON Formatter service at CuriousConcept, you'll see the structure. Here's code that would display some of the JSON data:

println json.name
println json.id
json.fields.each {
  println it
}

By the way, there's a breaking change in version 0.5.0 of HTTPBuilder that is relevant to this code. As the RC-1 release announcement states,

The HTTPBuilder class' URL property has been renamed to uri

So, if you move to 0.5.0 at some point, you'll need to use uri.path instead of url.path

Solution 2

If you just want to fetch the data, you could do it in Grails this way:

import grails.converters.*;

def url = new URL("http://neotest.dabbledb.com/publish/neotest/f820728c-4451-41f6-b346-8cba54e52c6f/projects.jsonp")
def response = JSON.parse(url.newReader()) // response is an instance of JSONObject (see Grails API docs)

println response.toString(3) // Pretty-printed output
response.each { key, value ->
    println "$key = $value"
}

(just as a simple alternative)

Share:
17,971
Kivus
Author by

Kivus

Former Software Engineer in both Charlotte and Raleigh / Durham, now learning the law at UNC.

Updated on July 06, 2022

Comments

  • Kivus
    Kivus almost 2 years

    I'm trying to access a site via their JSON export.

    The URL is: http://neotest.dabbledb.com/publish/neotest/f820728c-4451-41f6-b346-8cba54e52c6f/projects.jsonp

    I'm using HTTPBuilder to try and accomplish this in Groovy, but am having trouble. I used the example code from http://groovy.codehaus.org/HTTP+Builder to come up with this:

    // perform a GET request, expecting JSON response data
    http.request( GET, JSON ) {
        url.path = 'publish/neotest/f820728c-4451-41f6-b346-8cba54e52c6f/projects.jsonp'
    
        // response handler for a success response code:
        response.success = { resp, json ->
            println resp.statusLine
    
            // parse the JSON response object:
            json.responseData.results.each {
                println "  ${it.titleNoFormatting} : ${it.visibleUrl}"
            }
        }
    }
    

    However, when I run the unit test for the method I simply get No such property: GET for class: ProjectController groovy.lang.MissingPropertyException: No such property: GET for class: ProjectController which I'm having trouble understanding.

  • Kivus
    Kivus over 14 years
    I'm not wedded to HTTPBuilder by any stretch, it was just what a few of the top Google entries suggested for JSON calls from Grails. If there's another solution you'd recommend, I'm all for it, since I'm not making much progress here.
  • Rob Hruska
    Rob Hruska over 14 years
    A lot of people recommend Apache's HttpClient, which HTTP Builder appears to be based on, so it's probably the best option. I was just trying to help solve what appeared to be a compilation error in your Groovy script. If HTTP Builder doesn't work, you could just write some straight Java in your Groovy that uses HttpClient, or even java.net.*, although it'd be a bit more code.
  • JesperSM
    JesperSM almost 12 years
    Careful here! This example will leak the reader (and in turn java.net.HttpURLConnection and with that, the socket!), and it will not use the character encoding specified by the remote server (instead it will use your platform's default encoding). In other words: URL.newReader is BAD in the general case