Hey folks,

I'm trying to return a single object, however any way I approach it, it seems that the entity is wrapped in an array.

For instance, the object I want to return should look something like this:

  {
    "screenBackgroundColor" : "#000000",
    "controlPanelColor" : "#084d98",
    "progressBarColor" : "#ffffff",
    "progressElapsedColor" : "#000000",
    "progressBufferingColor" : "#fedb32",
    "elapsedTextColor" : "#ffffff"
  }

However, this module wraps my object in an array like this:

[
  {
    "screenBackgroundColor" : "#000000",
    "controlPanelColor" : "#084d98",
    "progressBarColor" : "#ffffff",
    "progressElapsedColor" : "#000000",
    "progressBufferingColor" : "#fedb32",
    "elapsedTextColor" : "#ffffff"
  }
]

This causes problems on with the player I'm using (BitDash) as the player expects an object and not an array.

I also tried with the "Object Arrays", which is close! but the entity is preceded by an object declaration of "0", like this:

{"0":{
"screenBackgroundColor":"#000000",
"controlPanelColor":"#084d98",
"progressBarColor":"#ffffff",
"progressElapsedColor":"#000000",
"progressBufferingColor":"#fedb32",
"elapsedTextColor":"#ffffff"
}}

It would be great if there was a way to output something similar to the first codeblock.

Use Case: Bitdash accepts skinning of its player using JSON. The path to the json file can be provided and to bitdash which it then parses. Example bitdash config:

...
style: {
  width:          '100%',
  aspectratio:    '16:9',
  controls:       playerControls,
  autoHideControls: autoHideControls
}
skin : "http://mydrupalsite.com/pathto/jsonview",
cast: {
  enable: true,
},
...

But since the file is returning an array, the skin configuration doesn't work... any help would be greatly appreciated.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ydahi created an issue. See original summary.

ydahi’s picture

Title: Return with array when only single value (json) » Return object only single value (json)
studio-days’s picture

I'm having a similar problem.

As a feature request, when setting the Pager settings in the View Display to only display 1 item, the opening and closing [ ] should be removed from the output.

Is that possible?

anthonylindsay’s picture

Category: Support request » Feature request

Let's change this to a feature request because as it stands it's not possible due to the fact that Views Datasource (and Views, I expect) needs to handle an unknown number of results. Then we can discuss the merits of the suggestion.

I'm not sure that choice of pager settings should actually change the data structure, but maybe a different setting could be squeezed in somewhere else.

spazfox’s picture

+1 for this feature. I need it to get Drupal to feed some data to Groove using their API (https://help.groovehq.com/knowledge_base/topics/how-to-setup-the-custom-...).

vlad.dancer’s picture

Status: Active » Needs review
FileSize
5.61 KB

Hi, folks. I've write a patch for this. Please review it.

I've added a new json format for that. Also add a form condition when this format will be available.
It returns only one object from view's rows so be careful - you should set only one item to return (via pager) to avoid unnecessary load.
See more info in README.txt inside a patch.

spazfox’s picture

Patch in #6 works perfectly for me. Thanks, vlad.dancer!

vlad.dancer’s picture

@spazfox Please, change issue status to the "Reviewed and tested by the community" if you've successfully tested.

spazfox’s picture

Status: Needs review » Reviewed & tested by the community

anthonylindsay’s picture

Version: 7.x-1.0-alpha2 » 7.x-1.x-dev
Status: Reviewed & tested by the community » Fixed
anthonylindsay’s picture

Status: Fixed » Closed (fixed)

closing

vlad.dancer’s picture

Thanks, @anthonylindsay. But I see now in the dev version an odd file: views_plugin_style_json.inc.orig I believe you should delete it.

anthonylindsay’s picture

Oops, Good catch, @vlad.dancer!

Errant file deleted.

iamfredrik’s picture

I need the object wrapped in an array like it was from the beginning. How do I get that feature back? I think there should be a setting so that you can choose the output.

Edit: works as designed. This was for single row output. I need it for single value field output.