I can't be sure if this is a bug but JSON returned for views services doesn't seem to take data type into account. It would be good to be able to set the data type for each field.

For example, a view that returns a list of node ids may return JSON like this:

{ nid: [ "1", "2", "3" ] }

But we want:

{ nid: [ 1, 2, 3 ] }

Comments

ygerasimov’s picture

Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Category: bug » support
Status: Active » Fixed

Formatter that returns json simply does json_encode. So you need to make sure that values that are returned by views are integer type and not string. I would try to do this with hook_views_post_execute(&$view) for example.

Status: Fixed » Closed (fixed)

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

drupalove’s picture

Issue summary: View changes

Since the module implements drupal_alter() this works for me:

function my_module_services_views_execute_view_alter($output, $view) {
  if ($view) {
    foreach ($output as $items) {
      foreach ($items as &$item) {
        if (isset($item) && is_numeric($item)) {
          $item = (int) $item;
        }
      }
    }
  }
}

Thanks.

timmay’s picture

Drupalove,
I'm trying to implement your fix in #3, but haven't gotten it to work yet. Are you inserting the function into services_view.module?
Thanks!

drupalove’s picture

Hi Timmay,
You need to insert it in your own module in the .module file. Then enable the module and clear the cache. To create a module you need to create a my_module.info file and a my_module.module file with the above hook. Place both files inside a directory named my_module.
See creating Drupal modules: https://www.drupal.org/developing/modules/7
Please confirm if this works for you.
Thanks.

timmay’s picture

Thank you,
I haven't had time to try this yet, and in the meantime figured out how to parse the strings into numbers in the framework I'm using. I will try and implement this at some point and confirm with you then.
Thank you!