Hi,
I need a custom output format for Json as my 3rd party javascript application requires that. Is there a way to get that done ?

Below is a sample format which I need it in.

[
{'title':'Heavy Metal', 'results': [
['/metal/1', 'Disturbed - The Game', 'icons/metal.png'],
['/metal/2', 'Marilyn Manson - The Beautiful People', 'icons/metal.png'],
['/metal/3', 'Soil - 2 Skins', 'icons/metal.png'],
['/metal/4', 'Alestorm - Wenches & Mead', 'icons/metal.png']
]},
{'title':'Pop', 'results':[
['/pop/1', 'Michael Jackson - Bad', 'icons/pop.png'],
['/pop/2', 'Britney Spears - If U Seek Amy', 'icons/pop.png'],
['/pop/3', 'Take That - Relight My Fire', 'icons/pop.png'],
['/pop/4', 'Rick Astley - Never Gonna Give You Up', 'icons/pop.png']
]},
{'title':'Folk', 'results':[
['/folk/1', 'The Proclaimers - I\'m Gonna Be', 'icons/folk.png'],
['/folk/2', 'Bob Dylan - Hurricane', 'icons/folk.png'],
['/folk/3', 'Jason Mraz - Geek In The Pink', 'icons/folk.png'],
['/folk/4', 'Beirut - Nantes', 'icons/folk.png']
]}
]

Cheers,
Vishal

Comments

allisterbeharry’s picture

Component: User interface » Code
Assigned: Unassigned » allisterbeharry
Category: feature » support

Well the first thing would be to define your view to have the data you want. Could you give me an idea of what schema your data has (what are title, results, etc.) and like what data types are you using - CCK ?

vishalkhialani’s picture

Hi,
I need only the below data output its rather simple.

user name - which I am successful in getting the json with by typing print $node->name
In the profile section I have created a text field named profile_industry1 but I don't know the variable name to use to put in the veiws node tempalte I tried $node->profile_industry1 but there is no output.

I am pretty new to drupal development so I don't know how to find the variable I need to use for this field.

Cheers,
Vishal

IrishGringo’s picture

any tricks on including the URL of an ImageCache Image in an XML file of some type?

allisterbeharry’s picture

The next release of the JSON plugin will allow tweaking the JSON output using a template file. As long as the data you want to include exists as view fields, you'll be able to customize the JSON output using views-view-json-style.tpl.php and views-view-json-row.tpl.php in the theme subdirectory.

allisterbeharry’s picture

Status: Active » Fixed

In 6.x-1.0-beta1 you can override the default templates for JSON and customize the output however you'd like.

Status: Fixed » Closed (fixed)

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

praimmugen’s picture

Version: 6.x-1.0-alpha3 » 7.x-1.x-dev
Status: Closed (fixed) » Active

Hi! Sorry to reopen this very old issue, let me know if I should instead open a new one, but my support request is strictly related to this.

With the 7.x dev version, I am trying to achieve a similar result as the original post. In my custom module I have overridden theme_preprocess_views_views_json_style_simple, but when I execute the view, nothing useful is returned. Instead, I obtain this output : {"data":[{"":null}]}. I get a bunch of Trying to get property of non-object error messages. In fact, when I run the preprocess function from whithin my module, this is the content of $var['rows'] :

[data] => Array
        (
            [0] => Array
                (
                    [field_name1] => value1
                    [field_name2] => value2
                    [field_name3] => value3
                )
            [1] => Array
                (
                    [field_name1] => value1
                    [field_name2] => value2
                    [field_name3] => value3

If I run the original preprocess function of views_json module everything is fine, $vars['rows'] contains the array of array of objects representing the fields, and the data are outputted correctly in the JSON. Except that that's the output I need to change... :P

I think maybe I could just edit my overriding function to handle the array I'm getting, but I'm not sure that this is normal.

Could you please tell me If I'm doing something wrong by overriding that function in my mymodule.module file? Thank you very much.

praimmugen’s picture

Version: 7.x-1.x-dev » 6.x-1.0-alpha3
Status: Active » Fixed

Update:
I see now that the difference is that $vars['rows'], when the function is called from whithin views_json, contains the "Array of row objects as rendered by _views_json_render_fields" (from the function comment), while when overriden inside my module contained something different, i.e. just the items resulting from the query (which was enough for me). I just used that.

If someone is interested, I've also managed to override the function just for a specific view by following this advice: http://drupal.stackexchange.com/a/53388/9843

Cheers :)

Status: Fixed » Closed (fixed)

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

Djaps’s picture

Issue summary: View changes

thanks @praimmugen just what i was looking for! :)