I extracted data in JSON Format from a view (using views_datasource). This is the snap-shot of preview of view.

enter image description here Now I want to use this data as php array/object so that I can access each field individually and use it. I tried this in my html.tpl.php file (just to see if the data is fetched or not):

    $vdata = views_embed_view('view_name');
    $json_data = drupal_json_decode($vdata);
    $thedata = $json_data->nodes->node->title;

    print ($thedata);

But this code is not printing anything. Showing error:"trying to get property of non object".

I am new to JSON thing so I dont know if that is the correct way to extract the data from it.

I would be very grateful if I could see a sample of code which does extraction of data and use it as array. Thank you.

  • Use print_r($vdata) and print_r($json_data) to see if it's working correctly. Is the 'datasource' format part of the Default display? If a different display outputs the datasource, then you need to specify it in your views_embed_view, like views_embed_view('view_name','display_name'); Commented Aug 17, 2012 at 23:39
  • I tried: print_r($vdata) and its printing something like {"nodes":[{"node":{"title":"Graduation Day Photo Four","body":"Gra .............................................. and so on. But $json_data and $thedata are not printing anything. and also the drupal_json_decode() is showing the same error. Thank you. Is it okay to use views_embed_view() in this case or I should use something else ? Commented Aug 18, 2012 at 5:37
  • Not sure of your specific need, but you can always load a view using $view = views_get_view('sample_view'); If all you're after is the data of a view, you don't need to use the view_datasource module at all. Commented Aug 20, 2012 at 16:06
  • Check out: drupal.org/node/48816 Commented Aug 20, 2012 at 16:06
  • @JohnathanElmore . Hey sorry for late response, as I postponed this task for few days. I tried insertview module. But this is not exactly my need. I want to use the data in JQuery that I have to write in template.php. So I want to get the data in php array format. I also tried views_get_view() . But this does not return what I want to utilise. The data above in the picture is exactly what I want but in php array format. Thank you. Commented Aug 30, 2012 at 8:59


Your Answer

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

Browse other questions tagged or ask your own question.