Expand my Community achievements bar.

SOLVED

Overriding json format based on certain condition

Avatar

Level 9

I have a jsp component where I am displaying the page based on whether some node attributes condition. I have created a.json.jsp for the same page but I need to read the same json format from out of box path if those conditions are not met. If I call the page path /content/abc/a.json, it will be recursive call to same page in my local json page.

How do I tell cq5 to use the default out of box json, not my local json.

There is one out of box url to call page info http://localhost:4502/libs/wcm/core/content/pageinfo.json?path=content/abc/a

but this gives page properties, Is there any url to get page content like we get when calling /content/abc/a.json 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You can achieve this by using Sling selectors to differentiate between your custom JSON and the out-of-the-box JSON. Create a new JSP script (e.g., a.customjson.jsp) that generates the default JSON response when your custom conditions are not met. In your JSP component, handle the JSON request accordingly by including either your local a.json.jsp script for custom JSON or a.customjson.jsp for the out-of-the-box JSON based on the conditions. Access your custom JSON as usual by requesting /content/abc/a.json, while the out-of-the-box JSON can be accessed with /content/abc/a.customjson.json, where customjson is the selector you created. This approach allows you to serve different JSON responses from the same page based on the conditions, ensuring flexibility and better control over your application's behavior.

 

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

You can achieve this by using Sling selectors to differentiate between your custom JSON and the out-of-the-box JSON. Create a new JSP script (e.g., a.customjson.jsp) that generates the default JSON response when your custom conditions are not met. In your JSP component, handle the JSON request accordingly by including either your local a.json.jsp script for custom JSON or a.customjson.jsp for the out-of-the-box JSON based on the conditions. Access your custom JSON as usual by requesting /content/abc/a.json, while the out-of-the-box JSON can be accessed with /content/abc/a.customjson.json, where customjson is the selector you created. This approach allows you to serve different JSON responses from the same page based on the conditions, ensuring flexibility and better control over your application's behavior.