consuming json with sinatra?

1,587 views
Skip to first unread message

Neville

unread,
Jul 27, 2009, 7:39:37 PM7/27/09
to sinatrarb
Hi,

I'm planning to write a 100% ajax javascript client using ExtJS which
will GET/PUT json to my server api.

I was planning to use Rails or Merb on the server, as I am familiar
with easily consuming json in those frameworks.

But Sinatra seems a better fit for my web service api, so I'm thinking
about making a prototype.

Anyhow, does anyone have any pointers on how to consume JSON with
Sinatra please?

Neville

Christopher Schneider

unread,
Jul 27, 2009, 9:54:39 PM7/27/09
to sina...@googlegroups.com
You can probably just use the json library to get a hash & arrays out
of it. Something like:

require 'json'

post '/post_something' do
my_hash_object = params[:json].from_json
end

Neville

unread,
Jul 28, 2009, 3:52:02 AM7/28/09
to sinatrarb
Thanks for the idea.

The JSON would come in as the body of the POST however, not as a named
parameter

On Jul 28, 11:54 am, Christopher Schneider <ch...@christopher-

Jonathan Stott

unread,
Jul 28, 2009, 4:00:06 AM7/28/09
to sina...@googlegroups.com
On Tue, 28 Jul 2009 00:52:02 -0700 (PDT)
Neville <neville...@gmail.com> wrote:

>
> Thanks for the idea.
>
> The JSON would come in as the body of the POST however, not as a named
> parameter
>

JSON.parse(request.body.read)

Regards,
Jon

Neville

unread,
Jul 28, 2009, 7:00:12 AM7/28/09
to sinatrarb
Thanks Jon,

I'll give that a try.

On Jul 28, 6:00 pm, Jonathan Stott <jonathan.st...@gmail.com> wrote:
> On Tue, 28 Jul 2009 00:52:02 -0700 (PDT)
>

Brian Hammond

unread,
Jul 28, 2009, 10:50:10 AM7/28/09
to sinatrarb
Is there an easy way to call .to_json on the response from a handler?

Consider 100 of these:

get '/' do
"foo".to_json
end

vs.

get '/status' do
{ :status => "you won" }
end

and the hash returned in /status somehow automatically has .to_json
called on it?

This is obviously a convenience thing I'm looking for. :)

Neville

unread,
Jul 28, 2009, 7:27:47 PM7/28/09
to sinatrarb
yes, that is the sort of json support I was hoping to see, but its not
a deal breaker

On Jul 29, 12:50 am, Brian Hammond

Christopher Schneider

unread,
Jul 28, 2009, 7:44:19 PM7/28/09
to sina...@googlegroups.com
Something along the lines of this should work.

def json_get(route, options={}, &block)
get route, options do
block.call.to_json
end
end


Then use it like:

json_get "/users/:id" do
{:name => "cschneid"}
end

You'll probably need to play around with the .call method, providing a
binding or something. (I haven't tested this at all)

Chris

Roland Swingler

unread,
Jul 29, 2009, 5:04:56 AM7/29/09
to sina...@googlegroups.com
You could also experiment with a simple rack middleware that just
calls to_json on the upstream response body. Something like

class JsonMiddleware
def initialize(app)
@app = app # Your sinatra app
end

def call(env)
upstream_response = @app.call(env)
upstream_response[2] = upstream_response[2].to_json
upstream_response
end
end

or similar (note the above is completely off the top of my head, so
may not work as is).

I think you can then just call use JsonMiddleware in your sinatra app.

Cheers,
Roland

Neville

unread,
Jul 29, 2009, 6:25:14 AM7/29/09
to sinatrarb
> You could also experiment with a simple rack middleware that just
> calls to_json on the upstream response body.

Yep ... I just found CloudKit ... looks awesome!
Reply all
Reply to author
Forward
0 new messages