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!
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | patch_views_services-2198951-2.patch | 618 bytes | earelin |
| #11 | patch_views_services.patch | 751 bytes | mhmd |
Comments
Comment #1
akolahi commentedyou can try a custom module...
Comment #2
UksusoFF commentedthx! it's work for me
Comment #3
ajlow commentedThank 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
Comment #4
mihai_brb commentedIt should actually be:
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
Comment #5
ajlow commented@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!
Comment #6
mihai_brb commented@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:
On my views endpoint I also get the printed output.
So I assume you are doing something wrong ... :)
Mihai
Comment #7
akolahi commentedExcellent work Mihai :)
Comment #8
mesh commented@mihai_brb Should'nt it be excluding the empty fields (or arrays) completely? Even fields that are specifically 'Excluded from display' show up..
Comment #9
mesh commentedIn instead of setting them to an empty string -
This works with the views preview but in the json output the empty fields are still there.
Comment #10
mihai_brb commented@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
Comment #11
mhmd commentedSimple patch to the module fix that issue by setting the to $target_key = '' instead of array if cardinality = 1
Comment #12
taatg37 commentedThe patch provided in #11 solves the issue!!! ( tested on 7.x-1.1). Thanks for the good work.
Comment #13
tregismoreira commented#11 solved my problem too!
Thanks ;)
Comment #14
zterry95 commented+1 for #11 patch.
Comment #15
abbasl7 commentedIs 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.
Comment #16
earelin commentedThe 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.Comment #17
Romisha commented#16 worked for me. Thanks @earelin
Comment #18
kylebrowning commented