Get raw JSON or convert to JSON

30 views
Skip to first unread message

Alex Kahn

unread,
Feb 23, 2010, 3:18:50 PM2/23/10
to Grackle Development
Hi,

Is there a way, with Grackle, to convert API request results to JSON?
Or is there a way to prevent Grackle from converting API results to
OpenStruct? Basically, I'd like to make AJAX requests to my app, and
it would be easier to use this data if it is available to my
JavaScript as JSON.

Thanks,
Alex

Hayes Davis

unread,
Feb 23, 2010, 3:39:55 PM2/23/10
to grac...@googlegroups.com
Alex,

Grackle has the concept of Handlers for a request type. By default json requests are sent to a Grackle::Handlers::JSONHandler instance which converts it to the TwitterStruct you normally get from Grackle. However, there is actually a built in handler for just the case you're describing, you just have to tell Grackle to use it for JSON requests. Here's how you do that:

client = Grackle::Client.new(:handlers=>{:json=>Grackle::Handlers::StringHandler.new})
res = client.users.show.hayesdavis?
#res is a String of JSON

Let me know if you have any other questions.

Hayes

Alex Kahn

unread,
Feb 23, 2010, 4:30:15 PM2/23/10
to Grackle Development
Hi Hayes,

Thanks for the response. Glad to see that there is support for this in
Grackle! Do you think this information belongs in the README or
perhaps could be further documented in the code? If so, I could try to
lend a hand in that work.

Cheers,
Alex

Hayes Davis

unread,
Feb 23, 2010, 5:42:19 PM2/23/10
to grac...@googlegroups.com
Alex,

Thanks for the offer. There's a brief mention of it in the documentation for the Grackle::Client constructor but I think it would be great to expand on it in the readme... maybe in an Advanced Topics section at the bottom. If you want to add that, please fork and run with it. Let me know when you're done with a pull request and I'll happily merge it in.

Hayes
Reply all
Reply to author
Forward
0 new messages