[groovy-user] RESTClient, JSON & null values

199 views
Skip to first unread message

Richard Paul

unread,
Aug 22, 2009, 10:04:30 AM8/22/09
to us...@groovy.codehaus.org
I'm attempting to use the following snippet to print out all the
properties of a JSON object.

import groovyx.net.http.RESTClient;

def twitter = new RESTClient('http://search.twitter.com/')
def response = twitter.get(path:'search.json', query:[q:'dog'])

response.data.results.each {
    it.each { key, value ->
        println "$key = $value"
    }
}

However a problem arises when the value is 'null'.
e.g.

{
results: [
{
text: "some text",
to_user_id: null, <-- null value
from_user: "exampleuser",
...
}
]
...
}

When run this script outputs:

text = some text
Exception in thread "main" net.sf.json.JSONException: null object
at net.sf.json.JSONObject.verifyIsNull(JSONObject.java:2856)
at net.sf.json.JSONObject.isEmpty(JSONObject.java:2212)
at org.codehaus.groovy.runtime.InvokerHelper.formatMap(InvokerHelper.java:544)
at org.codehaus.groovy.runtime.InvokerHelper.toMapString(InvokerHelper.java:610)
at org.codehaus.groovy.runtime.InvokerHelper.write(InvokerHelper.java:467)
at groovy.lang.GString.writeTo(GString.java:188)
at groovy.lang.GString.toString(GString.java:160)
at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:540)
at org.codehaus.groovy.runtime.InvokerHelper.toString(InvokerHelper.java:112)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.println(DefaultGroovyMethods.java:571)
at groovy.lang.Script.println(Script.java:155)

I would expect it to output 'null' in this case but it appears to be
having problems formatting the value.
Any suggestions?

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Roshan Dawrani

unread,
Aug 22, 2009, 10:49:49 AM8/22/09
to us...@groovy.codehaus.org
I think you just need to check whether any JSONObject you are getting in the result or wrapping null objects or now. So, a variant of code below may help you avoid the error you were getting:

====================================================================================
import groovyx.net.http.RESTClient;
import net.sf.json.*

def twitter = new RESTClient('http://search.twitter.com/')
def response = twitter.get(path:'search.json', query:[q:'dog'])

response.data.results.each {
    it.each { key, value ->
        println "\t$key = ${getValue(value)}"
    }
}

def getValue(value) {
     if (value instanceof JSONObject && value.isNullObject())
        "<<null>>"
    else
        value
}
====================================================================================

HTH,
Roshan

Tom Nichols

unread,
Aug 22, 2009, 12:44:52 PM8/22/09
to us...@groovy.codehaus.org
No, I don't think the exception is because the 'value' being returned is null.

Are you sure the data structure you're describing is what's _actually_
being returned? I tell everyone this, but turn on 'wire' logging for
Apache HttpClient as described
here:http://groovy.codehaus.org/modules/http-builder/doc/index.html#Logging_and_Debugging

And verify that 'results' isn't null. That's what I would expect to
cause this error. If I have a chance, I'll try to do a quick test of
my own.

-Tom

Richard Paul

unread,
Aug 23, 2009, 2:48:53 PM8/23/09
to us...@groovy.codehaus.org
Thanks Tom, but the actual value being returned is null, you can see
an example at http://search.twitter.com/search.json?q=dog
-- JSONView makes it easier to read
https://addons.mozilla.org/en-US/firefox/addon/10869

Roshan, your solution works perfectly, thanks. I'll have to
investigate to find out why :)

Thank you both,
Richard.

2009/8/22 Tom Nichols <tmni...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages