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
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Himanshu_Singhal
Community Advisor
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

AEM 6.4.8
1 Accepted Solution
BrianKasingli
Correct answer by
Community Advisor
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
BrianKasingli
Correct answer by
Community Advisor
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

bsr060893
Level 3
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?
BrianKasingli
Community Advisor
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.
bsr060893
Level 3
Level 3
some how it is rendering the properties. So trying to understand is there any default model which will do the job
bsr060893
Level 3
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)
Shashi_Mulugu
Community Advisor
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