Make AEM page return JSON data | Community
Skip to main content
Level 2
March 29, 2017
Solved

Make AEM page return JSON data

  • March 29, 2017
  • 11 replies
  • 43276 views

[Thread Edited By Adobe]

/*Don’t forget to meet and greet your fellow peers virtually by telling them about yourself here

Go ahead and to it now: https://adobe.ly/3eDnB4v */

 

Actual Post:

hi Everyone,

My requirement is to do some business logic ((this can be only done in client side otherwise i could have written bundles)) in the javascript/jquery and take the output from that script and set that as page response in JSON format. Setting the AEM page response as JSON is the challenge.

I have tried using scriptlets like <%@ page contentType="application/json" %> and <%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>. But along with JSON it prints HTML also.

We want AEM page to return only JSON not any HTML. Any suggestions please ??

 

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 joerghoh

Hi,

assuming your page component (not the template but the page component) is located at /apps/myapp/components/mypage

then write a jsp /apps/myapp/components/mypage.json.jsp, which renders the page as JSON.

Then you can request your page located at /content/myapp/mypage (with resourcetype myapp/components/mypage) with "http://host/content/myapp/mypage.json to get your custom output. Another, potentially more flexible approach might be to write a servlet, bind it to the resourcetype myapp/components/mypage and a selector (for example "custom") and the extension "json" and then request the page via "http//host/content/myapp/mypage.custom.json".

Jörg

11 replies

joerghoh
Adobe Employee
Adobe Employee
November 29, 2017

Hi Richard,

I tried

localhost:4502/content/we-retail/us/en.body.html

and it returns only the body of the page, as rendered by /apps/weretail/components/structure/page/body.html.

I wonder what is different on your side.

Jörg

Level 2
August 4, 2023

AEM does not allow us to use the JSON extension.

We always have to use it on jcr:content.custom.json

when the extension is .html we can hit the page directly but if its anything other than that we have to hit jcr:content

 

Any clues as to why it behaves this way ?