Expand my Community achievements bar.

SOLVED

.model.json not showing component JSON at page level but only at specific component node level

Avatar

Community Advisor

Hi,

 

I've written JSON exporter with Sling model to expose it as JSON. When I try to drag the component on page and then check it with <page url>.model.json - it doesn't show the component JSON. But, when I get to specific node path e.g. <page-node-path>.model.json then it returns the component JSON. 
Any idea why it's happening like that?

 

Best regards,
Himanshu Singhal

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

6.4
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Himanshu_Singhal,

Apache Sling Models currently includes a single exporter, using the Jackson framework, which is capable of serializing models as JSON. Adobe's new core components are built with Sling Models, meaning that if you can easily build a headless AEM solution only using the core components. If you are using Adobe's core page component, layout container, responsive grid component in editable templates, you can replace ".html" with ".model.json", and you will get a JSON representation of the page structure (resourceType & all used components); assuming that you're Apache Dispatcher module rules allow you to access .model.json. e.g: https://example.com/home.model.json

Then there's no magic happening with the Jackson Exporter; all getter properties of your Sling Models class will exposed, and serialized to JSON. This means that if you run some kind of logic in your @PostConstruct method, then set the property, the computed value will be exposed in your JSON.

View solution in original post

8 Replies

Avatar

Correct answer by
Community Advisor

@Himanshu_Singhal,

Apache Sling Models currently includes a single exporter, using the Jackson framework, which is capable of serializing models as JSON. Adobe's new core components are built with Sling Models, meaning that if you can easily build a headless AEM solution only using the core components. If you are using Adobe's core page component, layout container, responsive grid component in editable templates, you can replace ".html" with ".model.json", and you will get a JSON representation of the page structure (resourceType & all used components); assuming that you're Apache Dispatcher module rules allow you to access .model.json. e.g: https://example.com/home.model.json

Then there's no magic happening with the Jackson Exporter; all getter properties of your Sling Models class will exposed, and serialized to JSON. This means that if you run some kind of logic in your @PostConstruct method, then set the property, the computed value will be exposed in your JSON.

Avatar

Level 3
what if a scenario where i dont have the sling model for a component and it is still fetching the properties in the model.json. What acts as the default exporter for such components?

Avatar

Community Advisor
if you have no sling model for a component, then the page will not be able to list the properties for your component, within the page.

Avatar

Level 3
some how it is rendering the properties. So trying to understand is there any default model which will do the job

Avatar

Level 3
getChildModels is doing the trick, but i am not able to find the code how exactly the proeprties are being returned once it encounter the modelFactory.getModelFromWrappedRequest(slingRequest, child, modelClass)

Avatar

Community Advisor

Sling Model Exporter works based on resource type. As per your post, you have written Sling Model exporter for Component, so .model.json works on component only. If you need the Json at page level, develop a sling model with exporter annotations for Page Resource Type