Overriding json format based on certain condition | Community
Skip to main content
Community Advisor
October 16, 2015
Solved

Overriding json format based on certain condition

  • October 16, 2015
  • 1 reply
  • 561 views

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 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by BrianKasingli

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.

 

1 reply

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
July 21, 2023

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.