By default, Sinatra knows how to serve up a rendered html page. With a slight tweak, we can make it spit out Json too!
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:
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
json helper will then serialize your objects into Json, much to the delight of your clients!