AEM code snippets : export page data as json format using sling servlet

Posted in AEM Code Snippets, AEM Tutorials By Raj On October 15, 2017


Here, I am going to explain to you how to export page data as a JSON format using sling servlet, this is developed on AEM 6.3, so you need to have below OSGI dependencies in your pom.xml file, if you created your project using AEM project archetype version 12 then you don’t  need to add these, by default it comes

Step 1: Add dependencies

Step 2: Import packages

You need to import below packages in your servlet.class file

Step 3: Create a  servlet

Here, I am not using separate class to load configurations, if you have so many properties to configure then I would recommend you to create a separate class and load it using @Designate annotation, want to know how to use @Designate annotation then refer my other article OSGI annotations – service

The below servlet reads title and path of all the child pages of configured root page and export it using JSON format, as we are exporting content as JSON format so we must add response.setContentType(“application/json”);

The buildLinks() method read all the child pages of configured page and populate list collection with the page information

Finally, I am using Google Gson to convert list collection object into a JSON string

Finally, you should see the output something like below


Download Code

 json export