Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to export page components as Json

Avatar

Level 4

Hi All,

 

I need to create a custom Servlet code where we need to iterate over the children and grandchildren of components on a certain page to create the output json of those components. We could do it using infinity.json OOTB , but we dont want to use the same as those are not recommended to be used in production . also we dont need to output all the components as json , we need to output as per the resource type.

 

Does anyone has any pointer or reference code to achieve the same.

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
8 Replies

Avatar

Community Advisor

Hi @tnik,

I think you do not need servlet to achieve your goal. Use Sling Model Exporters feature instead.

Please explore official Adobe documentation to get full set of information in terms of Sling Model Exporter concept and implementation guides:

Avatar

Level 4

Hi Lukasz-m , 

 

The issue is if we add sling model exporter to the components , we need to invoke them on 1 to 1 basis as per the component path , if we hit the Page path , it does not return the json for the components. We have already tried that

 

Thanks

Avatar

Community Advisor

hi As mention by @lukasz-m , model.json must do a job.

but if you think you need a custom solution, I created something similar for PoC, you can test try that out

https://aemlab.blogspot.com/2019/07/get-json-response-of-aem-page.html 



Arun Patidar

Avatar

Level 4

@arunpatidar model.json is not returning the json for custom component on hitting the page directly , if we give the component path then it returns the expected json. 

 

Also regarding the POC you did , does it exports all the custom properties of all the components on the page?

 

Thanks

Avatar

Level 4

@arunpatidar 

I want to loop the nodes as per resource types , but each component has different structure so how did you manage the component nodes to fetch properties from each one of them

 

Thanks

Avatar

Correct answer by
Community Advisor

Avatar

Community Advisor

Hi,

Yes, it basically look for all the nodes inside jcr:content(similar to infinity.json) and export as json.

There is a possibility to blocklist properties



Arun Patidar

Avatar

Level 4

Thanks @arunpatidar , this what we were looking for , i assume the solution is compatible with AEMaaCS