The following two renders were created from the same JSON view with different arguments:

Render 1

{
  "nodes":
    [
      {
        "nid" : "76200",
        "node_type" : "vehicle",
        "node_vid" : "76200",
      }
    ]
}

Render 2

{
  "nodes":
    [
      {
        "nid" : "13972",
        "node_data_field_vehicle_make_field_vehicle_model_branch" : "XLT",
        "node_type" : "vehicle",
        "node_vid" : "13972"
      },
      {
        "nid" : "13971",
        "node_data_field_vehicle_make_field_vehicle_model_branch" : "XLT",
        "node_type" : "vehicle",
        "node_vid" : "13971"
      },
      {
        "nid" : "13970",
        "node_data_field_vehicle_make_field_vehicle_model_branch" : "XLT",
        "node_type" : "vehicle",
        "node_vid" : "13970"
      }
    ]
}

Comments

sbefort’s picture

I accidentally hit the preview button so this issue was saved prematurely. Render 1 shown above is invalid JSON because of a trailing comma here:

Render 1

"node_vid" : "76200",

The invalid JSON causes serious problems with Internet Explorer. Firefox will read JSON with trailing commas, but IE7 and IE6 will not.

Why does the trailing comma happen only sometimes with the same view? The problem is with this code in the views-view-json.tpl.php file:

views-view-json.tpl.php Line 25

$total_field_count = count((array)$node); //cast the node object to an array so we can count how many properties in itt

The problem with the above code is that it counts elements in an array, even if those elements are empty. Here is a print_r of Render 1's (array)$node before the JSON is built.

Render 1 Array with 4 Elements

Array
(
    [nid] => 76200
    [node_data_field_vehicle_make_field_vehicle_model_branch_value] => 
    [node_type] => vehicle
    [node_vid] => 76200
)

There are four elements in the array, so $total_field_count will hold four. However, when the JSON is built, this will cause problems because [node_data_field_vehicle_make_field_vehicle_model_branch_value] is empty and will be excluded from the final JSON. We have an array with four elements, but the final JSON only contains three fields. Hence, the extra comma:

Render 1 Again

{
  "nodes":
    [
      {
        "nid" : "76200",
        "node_type" : "vehicle",
        "node_vid" : "76200",   <----- Trailing Comma
      }
    ]
}

So, anytime a user creates an optional CCK field that may be empty, the potential exists for this empty field to cause invalid JSON.

The problem can be fixed with this code:

$total_field_count = 0;
foreach ((array)$node as $key => $value) {
  !empty($value) ? $total_field_count++ : FALSE;
}

Now we are only counting array elements that have a value. No more trailing commas. Problems with Internet Explorer are solved.

yang_yi_cn’s picture

Where is the views-view-json.tpl.php ? I can't find it

ZeiP’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)

A similar problem doesn't seem to exist in the 7.x version. As D6 has been EOL for a number of years, closing this as outdated.