Hi,
I need to use json for one of the JavaScript applications. I found "views node feed" as the perfect module to get my views output as json.

The thing is I don't know how to format in the interface the module provides. I am new to this stuff but I am sure I can get hang of it easily.

Please see the user interface which the module provides but I can't understand how to use it and searched but could not find any help on the documentation.

Please see the attached image as to where I am stuck http://imagebin.ca/view/y9wEL5.html

I need the data to be in the below format for my javascript to work.

[
{'title':'Heavy Metal', 'results': [
['/metal/1', 'Disturbed - The Game', 'icons/metal.png'],
['/metal/2', 'Marilyn Manson - The Beautiful People', 'icons/metal.png'],
['/metal/3', 'Soil - 2 Skins', 'icons/metal.png'],
['/metal/4', 'Alestorm - Wenches & Mead', 'icons/metal.png']
]},
{'title':'Pop', 'results':[
['/pop/1', 'Michael Jackson - Bad', 'icons/pop.png'],
['/pop/2', 'Britney Spears - If U Seek Amy', 'icons/pop.png'],
['/pop/3', 'Take That - Relight My Fire', 'icons/pop.png'],
['/pop/4', 'Rick Astley - Never Gonna Give You Up', 'icons/pop.png']
]},
{'title':'Folk', 'results':[
['/folk/1', 'The Proclaimers - I\'m Gonna Be', 'icons/folk.png'],
['/folk/2', 'Bob Dylan - Hurricane', 'icons/folk.png'],
['/folk/3', 'Jason Mraz - Geek In The Pink', 'icons/folk.png'],
['/folk/4', 'Beirut - Nantes', 'icons/folk.png']
]}
]

Hope someone can help me out here its been two days I have been working on this.

Cheers,
Vishal

Comments

vishalkhialani’s picture

Hi,
I think I need to use the variables there I got an update from http://ivansotof.com/2009/10/drupal-modules-list

but I still can't get the data on my json file it turn out empty. I used something like. I am not sure where I am going wrong.

{
"nodes":
[
{
"uid" : " echo $nodes->uid ",
"users_name" : " print $nodes->users_name "
},
]
}

redcrackle.com

WorldFallz’s picture

I think the http://drupal.org/project/views_datasource module does this much easier-you just select a style for the view. Might be worth a try.

vishalkhialani’s picture

Hi,
I tried that module it works well but you don't have the option of formatting your json file which I need to do for my JavaScript application.

redcrackle.com

vishalkhialani’s picture

Hi,
Would apprecaite it if someone can help me out here with the views node feed module.

Cheers,
Vishal

redcrackle.com

WorldFallz’s picture

With only a couple hundred users, the odds of getting the attention of one of them in the forums are pretty slim-- you're better off posting to the module's issue queue.

vishalkhialani’s picture

Hi,
Thank you for the advice I have done so. Hopefully should get some feedback.

Also is there a professional help service which I can use and pay for as run into alot of small problems and I am not a full time programmer.

Cheers,
Vishal

redcrackle.com

vishalkhialani’s picture

Hi,
I managed to get some output when I changed the views from user based to node based and changed the varaible output to $node->nid

Is there a way I can user details on a nodes view type ? or what would be the variables to access users details.

Once again I am new to this and any help would be highly appreciated.

Cheers,
Vishal

redcrackle.com

oliverpolden’s picture

I would create a custom module. You can use the query from views to put into your module. Then you can build the array exactly as you want. You will need to create a menu callback so that when you go to: some/address/json it fires your function and returns drupal_json($your_array);

There are a load of tutorials about how to create a module. Basically create your module folder and in that you need your_module.info and your_module.module, you will also need to read about doing queries in Drupal.

I've created some code below which should be all you need to do this.

If you need to pass some values to your query then you can use %, and then accordingly set 'page arguments' e.g. if you wanted to pass the first and second argument in the url we would put 'page arguments' => array(0, 1), below I am passing the 3rd argument.

function your_module_menu() {
$items[some/address/%/json'] = array(
'page callback' => 'your_function',
'access callback' => TRUE,
'page arguments' => array(2),
'type' => MENU_CALLBACK,
);
return $items;
}

function your_function($argument) {
$sql = YOURSQL;
$result = db_query($sql, $argument);
// Now do some stuff with the result array to create your own array
return drupal_json($your_output array);
}

WorldFallz’s picture

You actually don't need a custom module. You can create a basic view that outputs the data required, then simply theme the view to produce the desired output. If performance is a concern, views can then export the view in a format suitable for a module.

oliverpolden’s picture

Just read your earlier posts. What might help you is to use the Devel module. With the Devel module enabled you can do stuff like dsm($nodes). I wouldn't bother trying to format your json output yourself. I would build the array in php and then use print drupal_json($your_array); to output the array in json.

In your views template (if that's what you're using) you would want to do something like:

$output_array = array();
foreach ($nodes as $node) {
$output_array[] = array(
'title' => $node->title,
'results' => array(
array('/metal/1', 'Disturbed - The Game', 'icons/metal.png),
array('/metal/2', 'Marilyn Manson - The Beautiful People', 'icons/metal.png'),
),
);
}
print drupal_json($output_array);

If you do:
dsm($nodes);
dsm($output_array);
you can see the structure of your arrays using Krumo (with the Devel module)

oliverpolden’s picture

Actually, looking here: http://ivansotof.com/2009/10/drupal-modules-list you need to use $node, not $nodes.

I would put the following in your template file and see what you get:
print drupal_json($node);

In your template file you could do some processing on the $node array before doing drupal_json. Not quite the right way to do it but it will work.

vishalkhialani’s picture

Thanks, for the feedback as I am new to this stuff I will need a day or so to work on your suggestions and hopefuly should give you guys the good news.

Cheers,
Vishal

redcrackle.com