Skip Navigation

[Resolved] Output a view as JSON

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Shane 4 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#1918951

Is there a way I can create a View of related posts where the output is JSON. I'm using a table plugin which has a JSON input.

#1919323

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jonathan,

Thank you for getting in touch.

The views plugin can give your data as a JSON output but this would mean passing the view shortcode into the Table plugin. If the table plugin is not able to handle a shortcode as an input then it won't work.

To create a view with a JSON output you will need to use the classic version of the view editor. To enable this go to Toolset->Settings and scroll to editing experience. Select the 3rd option and then refresh the page.

You should now get a new menu item at Toolset -> Views. Create your view from here.

In the view loop editor you're going to replace the existing wpv-loop tags with the ones below.

<wpv-loop>
			[wpv-item index=other]
				{"value": "[wpv-user field='ID']", "label": "[wpv-user field='user_login']"},
			[wpv-item index=last]
				{"value": "[wpv-user field='ID']", "label": "[wpv-user field='user_login']"}
		</wpv-loop>

Now all you need to do is replace the value and the label values with the appropriate shortcode for your output and then ensure that the "Disable the wrapping DIV around the View " option is selected.

I would highly recommend checking with the support team of the table plugin to see if it can handle shortcodes as an input first.

Thanks,
Shane

#1919469

The Table plugin requires a URL for a page which which contains the JSON so the shortcode isn't going to work.

In the code example you have show, I'm not 100% sure what you mean when you say 'replace the value and the label values with the appropriate shortcode for your output '.

What are you referring to when you 'say appropriate shortcode for your output'. Do you mean the shortcode for the field I wish to display?

#1919519

I've created a view: the loop looks like this:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-post-body view_template="loop-item-in-json-test-v2"]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

The fields look like this:

[types field="company-name"][/types]
[types field="forename"][/types]

What would the loop look like to turn this into JSON?

#1919589

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jonathan,

Yes this is what I meant, replace mine with the values you want.

So in your cause it will be.


<wpv-loop>
            [wpv-item index=other]
                {"value": "[types field='company-name'][/types]", "label": "[types field='forename'][/types]"},
            [wpv-item index=last]
                {"value": "[types field='company-name'][/types]", "label": "[types field='forename'][/types]"}
        </wpv-loop>

Just this section alone needs replacing. Secondly given that it requires to be in a URL i'm not sure how you're going to achieve this.

Given that the use case of views with the JSON format is generally to be used as a shortcode.

Thanks,
Shane