The concept of .json, .xml, .txt, etc

As many of you know CQ-Sling provides the concept of rendering a page/node in different formats. If you don’t know what am I talking about, then open your browser and hit this url  http://localhost:4502/content/geometrixx-outdoors/en/men/coats.json and probably you will get a json response like this:

{“jcr:createdBy”:”admin”,”jcr:created”:”Fri Jun 15 2012 23:49:17 GMT+0530″,”jcr:primaryType”:”cq:Page”}

If you look carefully you will come to know that these are properties of the node. Sling is providing the node data in json format when we append the url with “.json”. In the same way the same is true for xml, txt, pdf.

Let me tell you that .json is enabled by default and .xml, .txt and .pdf are not enabled by default. To enable/disable the renderer go to felix console configuration tab and search for Apache Sling GET Servlet and check/uncheck the boxes to enable/disable the renderers.

To explain how this concept can be used in our application , let me take an example of populating a Dialog drop down with some dynamic values. I can do this in two ways:

  1. By using a servlet.
  2. By using the concept of .json (renders)

By using a servlet:

Let me first tell how I used to do it with Servlet:

In dialog,  create a dropdown widget (xtype: selection, type:select) and add a property as below:

Property name: “options

Property value:  “/bin/myoptionsproviderservlet”       (path of the servlet)

(note that this property should be added to the dropdown widget in the dialog)

Then I write a servlet which gets the dynamic data, forms a json object and populates the dropdown.

This is a simple case. Using this approach may be difficult when we need to pass a query parameter to the servlet .  Now we need some different approach which makes it easy when dealing with situation like this.

By using .json:

In dialog,  create a dropdown widget (xtype: selection, type:select) and add options property as below:

Property name: “options

Property value:  “$PATH.optionsprovider.json

Create a jsp file with name “optionsprovider.json.jsp” (it can be any name, but make sure you provide the same name in the options property as well) and write a json rendering code in your jsp, then the drop down will be populated by the output of the “optionsprovider.json.jsp”.

To know how it works, please go through sling selectors (I may post about sling selectors later in my blog).

Advantages we get from this approach is:

  1. The in built objects will be available if we include global.jsp.
  2. No dependency on a bundle resource (basically a servlet, which will be in a bundle).
  3. The json response can be cached and the performance can be improved.
  4. Avoiding writing servlets.

This way we can render xml, txt, etc. Let me know if any questions. Thanks!

2 responses to “The concept of .json, .xml, .txt, etc”

  1. Mahendra Avatar
    Mahendra

    Can you please attach the sample code over here, It will be very useful to the users to understand.

  2. Jane Avatar
    Jane

    I have follow your step, but it can’t work in Touch UI, is there any difference between classic UI and TouchUI ?

Leave a comment