Create content to display JSON

I’d like to create a page [“content”] that’s going to display some JSON. The JSON response is dependent on the type of HTTP request, and it is supposed to be generated by the part.

What’s the suggested, scalable solution to this problem in Enonic XP 6.0.0?

I would imagine a solution in which I can use the Enonic XP admin interface to drag a JSON part onto a JSON page region. However, as the page is going to display JSON, and this JSON view is also present in the admin view, drag and drop in the admin interface seems cumbersome…

Hmm slightly confused - do you want to create json from a page or part? What is your use-case? Have you considered using services?

I’d like to display in in a page, but generate it in a part. :slight_smile:

Basically I’d like to be able to use content.create() form a part (exports.post()) and return JSON that’s going to be consumed by JS…

Ok, so if The part is both producer and consumer of the json you would do the following:

  • implement export.post in the part
  • use componentUrl to post directly to the part
  • set contentType= application/json in the response object

To be able to use the part in Live Edit (when editing the page in admin), the part output needs to be HTML. But you could check the rendering mode in the part controller, and if request.mode == 'edit' then return the JSON wrapped in a <textarea> or <pre> tag, otherwise return the plain JSON and set contentType='application/json'.

Excellent tips, thank you very much! :slight_smile:

Maybe a service would be better. You can make a service to have a URL endpoint that will handle POST and/or GET and do whatever you want and then return dynamic JSON.

Sounds so. I wasn’t aware of the service feature! :slight_smile: