Creating a quick JSON feed in Drupal 7

So your client has asked you to create a quick JSON feed in Drupal to expose a particular piece of content, in this case lets just assume its a listing of all your nodes. Now normally it's probably best to use a powerful module like the services module (and you could even use views) however for this case let's just assume that your client doesn't want you to add too much module bloat to your site (and you want some good practise in using Drupal's APIs).

Start off with creating the .info file

First create a folder in your sites/all/modules folder (or sites/SITENAME/modules if you're using multisite) with the name of your module. In this example I'm just going to use the name "json_feed".

Next create a file in that folder that has the name "json_feed.info". It will need as a bare minimum the following inside it:

name = JSON Feed
description = JSON Feed 
core = 7.x

Create the json_feed.module file

Now you need to actually create the bread and butter of this project. The module file. Let's break it down into steps what we want to accomplish:

  1. We want a url that we go to that...
  2. Shows us a listing of nodes...
  3. In a JSON format

Step 1 - Hook Menu

We need to use the Drupal hook_menu to be able to actually create the menu routing for the URL. 

/**
 * Implements hook_menu().
 */
function json_feed_menu() {
  $items = array();
  $items['json/nodes'] = array(
    'title' => 'JSON Nodes',
    'page callback' => 'json_feed_page',
  );
  return $items;
}

Place this function in your .module file. What this does is create an entry in Drupals menu system for 'json/nodes' and tells Drupal to look for the function named 'json_feed_page' to tell it what to do when someone/something navigates to that page.

Step 2 - The actual function

Now we need to create the function that will:

  • Do a SQL query to generate the list of nodes
  • Put it into an array
  • Use Drupals drupal_json_output($var) function to output it properly.

So lets do that!

First, let's create the 'json_feed_page' function:

function json_feed_page() {

}

Now, we're going to have to create that SQL query to grab the list of nodes. In Drupal 7 the database abstraction layer has become a little bit more object orientated. This may seem confusing at first but it does has its benefits. Add this to our function:

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title', 'uid'));
$results = $query->execute()->fetchAssoc();

This will give us an associative array containing the nid, title and uid of the content we want. Now we want to turn that into JSON. To do that it's pretty simple. PHP has a function called json_encode but we're going to use the Drupal version of this, which is naturally called drupal_json_encode()

drupal_json_output($results);

So at the end our function should now look like:

function json_feed_page() {

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title', 'uid'));
$results = $query->execute()->fetchAssoc();

drupal_json_output($results);
}

Make sure that you clear your cache. Then navigate to /json/nodes and it you should get a nicely formatted JSON page.

Add new comment