Serve Json From Sinatra

| Comments

By default, Sinatra knows how to serve up a rendered html page. With a slight tweak, we can make it spit out Json too!

JSON on Sinatra

Data Format of the Web

Json is my favorite data format for web applications. Really, who wouldn’t want their webservice to expose Json? It’s mucho useful.

First, add a line to the Gemfile that makes a Json helper method available to Sinatra:

1
gem "sinatra-contrib"

Next, all you need to do is query the data that you want to return and actually return it, as in:

1
2
3
4
5
require "sinatra/json"

get "/my-models" do
  json MyModel.all
end

The json helper will then serialize your objects into Json, much to the delight of your clients!

Comments