Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Best way to expose existing pages as JSON?

therealsatz
Level 2
Level 2

Hi Guys, we have an existing website containing more than 2000 pages and we are looking as exposing the content as a service so that they can be consumed by an external mobile app.

1. I have tried looking at CaaS feature available OOTB in 6.4 but that requires us to create content fragments but we already have 2000 pages which needs to be exposed for a mobile app

2. I've also looked at JSON Exporter which works with the suffic ".model.json". This gives some json data but its not really usable since it is more of a dump of the jcr structure and also misses some important page content like Text, Images etc.

So apart from the above two options, do we have anything else that would fit the use case? We would just need our page content like text, images etc. to be exposed to be used by a 3rd party mobile application.

9 Replies
Jörg_Hoh
Employee
Employee

Regarding your approach 2): If you get the JCR properties as well, you are the seeing the output of the Default Get Servlet (in JSON format). This is the generic fallback of all requests to .json files. But this also means that you don't have a ModelExporter registered for this page.

To make the SlingModelExporter work, you can check the documentation at [1].

Jörg

[1] Adobe Experience Manager Help | Developing Sling Model Exporters in AEM

therealsatz
Level 2
Level 2

Hi Jörg, you were very right about the exporter not being registered. Thanks for that.

with that said, I created a exporter for the model as well but I believe the whole exporter feature works only when we access the components specifically but not for the whole page itself.

What we are trying to achieve is to give some form of meaningful json for a mobile app developer to consume for the whole page and not for specific components.

Veena_Vikram
Community Advisor
Community Advisor

Did u think about writing a custom servlet which will help you expose your content in your required format ?

therealsatz
Level 2
Level 2

Hi Veena, yes we gave that a thought but how well do you think that will work?

Not all our components have sling models associated with them and all our pages follow a different structure, also we have some sling models which has dynamic objects populated from external APIs.

Do do you think there is a simple strategy to get a structured json of a page via a servlet without having to write too much if else conditions taking the various page structures into consideratio?

Jörg_Hoh
Employee
Employee

If you don't have sling models in place for all components, the only chance you have is just to dump the JCR content structure (and then you don't get the additional semantics which are provided by the model classes, as they know much more about the actual content and its semantic than just the pure repo structure).

Basically this is done by the Default Get Servlet and its JSON rendition, but you can write a custom servlet which does this as well; and there you can omit certain properties from being delivered, e.g "jcr:primaryType" etc.

using OSGI r6 anotations it can look like this:

@Component(service=Servlet.class)

@SlingServletResourceTypes(

  resourceTypes="cq/Page",

  selectors="dump",

  extension="json"

)

public class MyServlet ....

(Apache Sling :: Servlets and Scripts )

rampai
Level 5
Level 5

Try this. It exposes AEM pages as json with the structure exposed as well.

The only known limitation here is that editable templates do not work in this.

Getting Started with AEM Sites - WKND Tutorial

therealsatz
Level 2
Level 2

Thanks for the response Rampai, i do not see anything related to exposing page data as json in the github project you had posted. have you posted the right link?