Hi, I need to change the JSON output from the actual:

{
    "nodes":[
          {"id":"7081" .....

to

{
   "status": "OK",
    "num_results": 10,
    "nodes":[
           {"id":"7081" .....

Someone can help me, I read the code but i dont'understand enough :(. How can I get these two lines?
Thanks

Comments

Alexander Allen’s picture

Title: views_json: Add support for document-level properties » Change JSON output

Hi Aress,

Neither nesting, nor top-level document properties are currently supported by views_datasource. In your example, "status" and "num_results" could be properties that belong to the JSON document itself. The most obvious way that I see this could be accomplished is by adding variables into the header settings for the View. I have tried doing that already, only to come across this exact same issue. Header variables are ignored by this module. As a result, I ended up using relationships to node reference fields, resulting in duplicate items like so:

{
 "nodes":[
    {
      "id":"7081",
      "status": "OK",
       "num_results": 10
     }, {
      "id":"5056",
      "status": "OK",
      "num_results": 10
      }....

My approach is only a temporary workaround (otherwise I would need to ditch views_datasource altogether). This needs to be fixed, urgently. It is frustrating for front-end developers to have to deal with the duplicated fields, and considering this module is used a lot for serving feeds into mobile devices, it also increases significantly the feeds' size on the wire.

Changed to task -> major.
Assigned to myself, as default. If anyone else wants to give a hand w/ this let me know.

Alexander Allen’s picture

Title: Change JSON output » views_json: Add support for document-level properties
Version: 6.x-1.0-beta2 » 7.x-1.x-dev
Assigned: Unassigned » Alexander Allen
Category: support » task
Priority: Normal » Major
Alexander Allen’s picture

Title: Change JSON output » views_json: Add support for document-level properties
Priority: Major » Critical
Issue tags: +#VDS-A, +#RVDA

Tagging and bumping priority. This is a major part of #RVDA.

elephant.jim’s picture

Issue summary: View changes
Status: Active » Needs review

I ran into this same problem. I wanted to use a views pager to limit the results returned, but still report the total count. The easy way is to add the "Global: Results summary" to the view header. But then that needs to get stuffed into the JSON ...

The patch I used for simple JSON output was:

diff --git a/views/theme/views_views_json_style.theme.inc b/views/theme/views_views_json_style.theme.inc
index c20712f..cd2ba18 100644
--- a/views/theme/views_views_json_style.theme.inc
+++ b/views/theme/views_views_json_style.theme.inc
@@ -105,7 +105,20 @@ function template_preprocess_views_views_json_style_simple(&$vars) {
   }

   // check if user wants nested arrays
-  $vars["rows"] = strlen($root_object) != 0 ? array($root_object => $objects) : $objects;
+  if (strlen($root_object) == 0) {
+    $vars["rows"] = $objects;
+  }
+  else {
+    $is_result_empty = empty($rows);
+    $vars["rows"] = array(
+      $root_object => $objects,
+      'header' => $view->display_handler->render_area('header', $is_result_empty),
+      'footer' => $view->display_handler->render_area('footer', $is_result_empty),
+    );
+    if ($is_result_empty) {
+      $vars["rows"]['empty'] = $view->display_handler->render_area('empty', $is_result_empty);
+    }
+  }
 }

 function template_preprocess_views_views_json_style_exhibit(&$vars) {

You'd also need to change template_preprocess_views_views_json_style_exhibit and template_preprocess_views_views_json_style_jqgrid to match template_preprocess_views_views_json_style_simple.

kenorb’s picture

Assigned: Alexander Allen » Unassigned
kenorb’s picture

Priority: Critical » Major
ZeiP’s picture

Category: Task » Feature request