20 years ago XML was a perfectly suitable choice for creating RSS feeds, but over the years developers have gradually started using alternatives to XML. One such alternative is JSON. Its simple syntax and structure make it easy for developers to work with and is commonly used in APIs all over the Internet. JSON Feed was developed by Manton Reece and Brent Simmons as a more developer-friendly way of building RSS feeds.
JSON feeds serve the same purpose as RSS feeds: They provide a file that lists the most recent changes to a website or collection of articles. JSON Feed is only a few months old, but RSS feed readers like Feedbin have already begun adding JSON Feed compatibility.
How do you add a JSON feed to a Ruby on Rails application, then?
In this example we’re using the same route, controller and action as the RSS feed we used in the previous example. For the purpose of this article, we’re going to have just one feed in our Rails application, the JSON feed.
Define the route
Like RSS feeds, we need to define a route for our JSON feed. In our Rails application’s
routes.rb file, we’re going to define the route to our JSON feed.
Create a controller for your feed
Right, we have our route defined. Next we want to connect the route to our ActiveRecord model that represents the changes in the website. Let’s create a controller to do that.
Just like our RSS feed, we’re going to create a feeds controller and give it an index action that will serve the content for our feed.
We’re responding to the index action’s request with a JSON file, so the
respond_to block will use the
.json method to return the content in the correct format.
Just like an RSS feed, we’re rendering the response without a layout. There’s nothing to the JSON feed file other than its own content. We don’t need a layout for this.
Create the view to return the feed
Last but not least is the view file that will contain the content of the feed in a format that meets the requirements of the JSON Feed standard. This means that the file will use a json extension and will differ slightly in the syntax used.
The view file will contain a number of elements similar to those of an RSS feed.
- a unique identifier for the article
- the body of the article
- a link to the article
Your view file will look something like this:
You’ll notice that the JSON feed has a lot less in it than the RSS feed example. For the purposes of this article, we are using a simple version, but even a more detailed JSON feed would still be quite simple.
Where your RSS feed view file has an
.rss.builder extension, this view file will use a
.json.jbuilder file extension. We’re using the JBuilder gem to generate our view file contents.
Finally, let’s make our JSON discoverable.
This differs slightly from an RSS feed in that we’re defining the response type because it’s going to be a JSON response.
What are the benefits?
While the adoption of the JSON Feed standard is still growing, you might question the benefit of adding a JSON feed to your Rails application.
There are many arguments for and against all standards used on the Internet, but I lean towards open standards that make accessibility of web sites and applications easy. It’s for that reason that while I would recommend adding a JSON feed to your website, it’s not an urgent thing to do. It’s just more of a nice thing to have.
If you’re building a web-based product, though, that uses feeds as a delivery method for content, then I would definitely put this nearer the top of your product’s next feature list.
The JSON Feed standard is still young, but as a more developer-friendly version of RSS, it’s worth looking into, especially if you know the consumers of your feeds are using JSON Feed compatible feed readers.
With new technology it’s always a case of ensuring there is high enough adoption before you start learning and using that technology yourself. However, the ease with which you can add a JSON feed into your Rails application means that it’s a low-risk feature that can be added to most Rails applications in little time.