1

I am trying to pull all articles from my site in form of JSON.

I know it can be done by either Services, RESTful Web Services or Views Datasource but I don't know how to do it. I am a complete n00b in Drupal.

Could you specify me, with an example hopefully, that how can I pull all articles in a JSON and show that JSON through a URL?

2
  • Which version of Drupal are you dealing with? Commented Apr 10, 2013 at 20:27
  • Drupal Version 7 Commented Apr 10, 2013 at 20:35

3 Answers 3

1

Thanks Alex, Thanks Sibiraj,

I was able to do it through Services + REST.

I created a view named content and configured it to show all the Published Articles.

This is what generated for me:-

http://myurl.com/views/content.json

Could I ask one more question, that how can I control how many articles could be shown. I am implementing Dynamic scrolling in my app and I want to show 10 Articles at a time. When a user scrolls till the bottom end, I want to show next 10 articles.

The problem with this URL is that it shows all the articles. Could I say append a variable with it, let's say through which it can control the start and end of article index?

http://myurl.com/views/content.json?/start_article=0&end_article=10

Many Thanks.

1

Judging by its description Views Datasource might be the way to go, but it seems to be still not ready for production. There is no even beta version as far as I can tell. But your task can be easily achieved by creating your own custom module.

Create your own module (you can read more about this process here).

And in your module do the following:

//Your implementation of hook_menu, create url endpoints here
function YOURMODULENAME_menu() {
    $items = array();
    $items['path/where/i/want/your/json/to/be'] = array(
        'title' => t('list of articles'), // can actually be arbitrary text here
        'type' => MENU_NORMAL_ITEM,
        'page callback' => 'your_function_name',
        'access callback' => TRUE,
    );
    return $items;
}

//Page callback function that renders the json endpoint
function your_function_name() {
    $query = "SELECT * FROM {node} n WHERE n.type = :type";
    $result = db_query($query, array(':type' => 'article'));
    $json_data = array();
    foreach ($result as $record) {
        $node = node_load($record->nid);
        $json_data[] = array('nid' => $node->nid, 'title' => $node->title, 'body' -> $node->body['und'][0]['value']);
    }
    print drupal_json_encode($json_data);
    exit;
}

I didn't test this code and you might have more fields in article datatype, but I think you should get the idea how this can be done. Just query the database for the list of nodes of specific type, compose an array of data and output json.

0

You can try some module like feeds or services or views_data_export or node_export

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.