I don't think this has been raised.

I've noticed that fields with empty values are being displayed in my JSON feed as an empty array. This is fine if my field holds an array however an empty array is always displayed no matter if the field is setup only to have one string value or if the field is an object (as in the case for an image field).

For instance, I have a description long text field set up in my Services View. If there is no value I get this:

"description":[
],

If there is a value it will be this:

"description":"This is it",

Likewise, I have an image field. If there is no value I get this:

"mediafile":[

],

If there is a value it will display my image object:

"mediafile":{
"fid":"118915",
"alt":"",
"title":"",
"width":"600",
"height":"200",
"uid":"1",
"filename":"blah_600x200.jpg",
"uri":"s3://blah_600x200.jpg",
"filemime":"image/jpeg",
"filesize":"88763",
"status":"1",
"timestamp":"1392071074",
"rdf_mapping":[

]
},

Is there a reason why an empty array is returned by default? I imagine it may have to do with safe values, however this causes confusion and headaches when third party consumers of my service are expecting, for instance, an object but gets an array.

Can Services View be updated so that for empty values:

  • " " - For a string field
  • [ ] - For an array field
  • { } - For an object field such as an image

Thank you!

Comments

akolahi’s picture

you can try a custom module...

function mymodule_services_views_execute_view_alter(&$output, $view) {
  if ($view->name == 'my_view' && $view->current_display == 'myview') {
    // Make sure we have results.
    if (empty($output) && !is_array($output)) {
      return;
    }

    // Loop trough all rows.
    foreach ($output as &$row) {
      foreach ($row as $field_name => $field_value) {
        // Replace all empty values with an empty string.
        if (empty($field_value) && strpos($field_name, 'field') === FALSE) {
          $row->$field_name = '';
        }
      }
    }
  }
}
UksusoFF’s picture

thx! it's work for me

ajlow’s picture

Thank you for the suggestion. I tried to implement the hook but it is not being called...

Am I doing something wrong?

I've got Services Views 7.x-1.0+4-dev

mihai_brb’s picture

It should actually be:

function mymodule_services_views_execute_view_alter(&$output, $view) {
  if ($view->name == 'my_view' && $view->current_display == 'my_display') {
    // Make sure we have results.
    if (empty($output) || !is_array($output)) {
      return;
    }
    // Loop trough all rows.
    foreach ($output as &$row) {
      foreach ($row as $field_name => $field_value) {
        // Replace all empty arrays with an empty string.
        if (empty($field_value)) {
          $row->$field_name = '';
        }
      }
    }
  }
}

The part with strpos($field_name, 'field_name') === FALSE) was only making sure that 'field_name' is not being altered and most probably is not needed. But in some cases a field should always return an object, for example a double field.

@ajlow is this hook not being called at all, or the results are not altered?

Mihai

ajlow’s picture

@mihai_brb

Thanks for the reply back.

The hook is not being called for some reason...

I've cleared the cache and have successfully called other hooks such hook_services_request_postprocess_alter and hook_views_query_alter.

I have the following with respect to services modules:

- Drupal 7.19 (yes it is an older verision)
- Services 7.x-3.5 (I have REST server and OAuth Authentication enabled)
- Services Views 7.x-1.0+4-dev

Thank you for your help!

mihai_brb’s picture

@ajlow,

I downloaded/installed:
Drupal 7.19
Services (services) 7.x-3.5
Services Views (services_views) 7.x-1.0+4-dev

Implemented custom module:

function MYMODULE_services_views_execute_view_alter(&$output, $view) {
  print_r($output);
}

On my views endpoint I also get the printed output.
So I assume you are doing something wrong ... :)

Mihai

akolahi’s picture

Excellent work Mihai :)

mesh’s picture

@mihai_brb Should'nt it be excluding the empty fields (or arrays) completely? Even fields that are specifically 'Excluded from display' show up..

mesh’s picture

In instead of setting them to an empty string -

        if (empty($field_value)) {
          unset($row->$field_name);
        }

This works with the views preview but in the json output the empty fields are still there.

mihai_brb’s picture

@mesh I didn't wanted to unset empty values, because the system accessing this endpoint assumes those keys are always sent, even empty. isset() is not that easy to implement in some programming languages.

Mihai

mhmd’s picture

StatusFileSize
new751 bytes

Simple patch to the module fix that issue by setting the to $target_key = '' instead of array if cardinality = 1

taatg37’s picture

The patch provided in #11 solves the issue!!! ( tested on 7.x-1.1). Thanks for the good work.

tregismoreira’s picture

#11 solved my problem too!
Thanks ;)

zterry95’s picture

Status: Active » Reviewed & tested by the community

+1 for #11 patch.

abbasl7’s picture

Is this issue specific to Service_views module?
Because I face this problem when I login a user using Services module. (I have custom fields in User Profile).
Can someone please confirm?
If this needs to be fixed with Services module, I'll create a bug for Services module.

earelin’s picture

StatusFileSize
new618 bytes

The patch does not work if the module is installed in other folder than sites/all/modules. I have changed the paths on the patch file so now is working with another installation folders as in a profile.

Romisha’s picture

#16 worked for me. Thanks @earelin

kylebrowning’s picture

Status: Reviewed & tested by the community » Fixed

  • kylebrowning committed 5e1a1be on 7.x-1.x authored by earelin
    Issue #2198951 by earelin, mhmd: Empty or no value, JSON Service always...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.