How can you transfer data between websites? There are many options and modules for Drupal out there. You can use the Services, RESTful Web Services or Views Datasource module. However, using those modules can sometimes be a bit overkill. If all you want to do is expose some data via JSON so that it's accessible for other websites, then it’s easier to create a custom menu item and just return the exposed data as JSON from a menu callback.

In this article, we'll look at encoding and decoding JSON within Drupal as well as creating a JSON endpoint for content.

Drupal offers two functions for encoding and decoding JSON. To encode an array into JSON use drupal_json_encode() and to decode a JSON object into an array use the – can you guess it? – drupal_json_decode() function.

Encoding JSON

First, let’s look at encoding an array into JSON. Take this array for example:

$array = array('Boat', 'Car', 'Ship', 'Building');

If we were to encode the above array into JSON, you will get ["Boat","Car","Ship","Building"] returned. Here is a code example:

$array = array('Boat', 'Car', 'Ship', 'Building'); $output = drupal_json_encode($array); // $output will be ["Boat","Car","Ship","Building"]

Decoding JSON

Now let's look at decoding a JSON object. If you were to running the following code:

$json = '["Boat","Car","Ship","Building"]'; return drupal_json_decode($json);

The drupal_json_decode() function will return an array.

So far we have looked at the drupal_json_encode() and drupal_json_decode() function and have seen how simple they are to use.

Pulling In JSON Objects

The next example I want to demonstrate is how to pull in a Twitter account using Twitter's GET users/show REST API.

First, we’ll need to get the JSON object and for this we’ll use the drupal_http_request() function:

$request = drupal_http_request('https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true');

Within the $request variable, there will be a data property $request->data . This stores the JSON object.

The only thing left to do is decode the data property to get access to all the information.

$request = drupal_http_request('https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true'); return drupal_json_decode($request->data);

Example of the decoded array:

JSON Endpoints

The last item I want to demonstrate is how to create a JSON endpoint for content on a Drupal site. For example, if you navigate to example.com/node/1/json the website will output a JSON version of the content. This will show you how to output a full JSON page using the drupal_json_output() function.

NOTE: If you want to create JSON or XML endpoints on a production site look at using the Services module.

1. First we must implement a menu item using the hook_menu. The menu item path needs to be "node/%node/json". For more details on how to use hook_menu check out the API documentation.

/** * Implements hook_menu(). */ function ww_json_example_menu() { $items['node/%node/json'] = array( 'page callback' => 'ww_json_example_endpoint', 'page arguments' => array(1), 'access arguments' => array('access content'), ); return $items; }

2. Create a "page callback" function for the menu item. Also, make sure that you add a $node variable to the function signature. For this example, the page callback is "ww_json_example_endpoint". Finally, we pass the $node object through to the drupal_json_output() function. If you go to node/1/json you should just see outputted JSON code.

function ww_json_example_endpoint($node) { drupal_json_output($node); }

If you have any questions please leave a comment below.

Further Resources