How can I embed the output of a view in another page, as a JSON Data Document, without stopping the page processing ?

I have a view with format set to JSON data document and am attempting to embed the block output in a node with views_embed_view('nec_map_data','block').

This embeds the block but with Views API mode unchecked page processing is stopped at the JSON and the page doesn't render.

If I enable Views API Mode then the block output is wrapped in a div.

I would like the JSON output from the view to be assigned to a javascript variable in the node. How can I achieve this ?

1 Answer 1


You can do that by adding this function ( a customized version of the original views_json_get in the view_datasource module ) in your template or custom module

 * Gets JSON data from a View rendered in the JSON data document style.
 * This is useful for when working with a JSON view in code.
 * @param $name
 *   The name of the view.
 * @param $display_id
 *   The display of the view to use.
 * @param $args
 *   The arguments to pass to the view.
 * @param $raw
 *   If TRUE, the JSON data is returned as a string.  Otherwise, an object
 *   representation is returned.
 * @return
 *   The JSON data in the form of an object or a string or NULL otherwise.
function views_json_noheader_get($name, $display_id = 'default', $args = array(), $raw = FALSE) {
  $view = views_get_view($name);
  if (!is_object($view)) {
    return NULL;

   $view->display[$display_id]->display_options['style_options']['using_views_api_mode'] = 1;

  $preview = $view->preview($display_id, $args);
  $start_pos = strpos($preview, '{');
  $finish_pos = strrpos($preview, '}');
  $length = $finish_pos - $start_pos + 1;
  $json = trim(substr($preview, $start_pos, $length));

  if ($raw) {
    return $json;

  return json_decode($json);

and get the result of your view by typing in your function:

$json = views_json_noheader_get('yourviewname', 'yourview json display id', null, true);

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.