Highlighted

How to Export JSON (Business Logic) of multiple components in a page using Sling Model Exporter

mayanky56670501

05-12-2018

I am not able export JSON of multiple components in a page using Sling Model Exporter.

If I am using http://localhost:4502/content/test.model.json, then I am getting the simple json of the Page.

If I am using http://localhost:4502/content/test.infinity.json then I am getting the Deep level JSON for all the Node Content.

I basically want to get the JSON for my business logic developed in JAVA, that to be of multiple components lying on the page.

Using @Exporter Annotation. I am able to get the JSON for Individual Component, but I am not able to get that JSON for multiple components.

Would be grateful if anyone could provide the correct solution!

Replies

Highlighted

Arun_Patidar

MVP

05-12-2018

Hi,

You can create exporter for multiple components but when you will access page using .model.json, it will return the JSON response from multiple components

Adobe Experience Manager Help | Understanding Sling Model Exporters in AEM

e.g.

http://localohost:4502/content/my-resource.model.json

Highlighted

mayanky56670501

05-12-2018

It will return only the JSON which is stored under the node /content/my-resource/jcr:content/...

But my question is how to Export the JSON of Multiple Components on a particular page that to be of a JAVA Based Logic, not the simple data which you put inside the Dialog.

Highlighted

mayanky56670501

05-12-2018

This is JCR Node Structure and via /content/my-page.infinity.json

I am getting a JSON basis of this Node Structure.

JCR Node Structure.PNG

JSON Structure on Node Basis!

JSON Sample.PNG

While on the other hand, a Sling Model Exporter JSON like..

Sling Model JSON.PNG

Just like this.. I want to Export the JSON for multiple components made using Sling Model @Exporter lying on the same page.. by simply calling /content/my-page.model.json or something!!

Highlighted

smacdonald2008

05-12-2018

I am not sure this is a possible use case - as discussed in this How To article - JSON is exported based on a single component.

Adobe Experience Manager Help | Exporting Adobe Experience Manager Data Using Sling Model Exporters

If it is possible - i have never seen the code to do so.

Highlighted

smacdonald2008

05-12-2018

If you look at the Java code in this article - you will notice that data members of the code lineup with component props. So its binding components props to data members. For example:

@Model(adaptables = Resource.class, resourceType = { "weretail/components/content/heroimage" }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

@Exporter(name = "jackson", extensions = "json", options = { @ExporterOption(name = "SerializationFeature.WRITE_DATES_AS_TIMESTAMPS", value = "true") })

public class ModelComponent {

    @Inject

    String title;

    @Inject

    String heading;

    @Inject

    String buttonLabel;

    @Inject

    String buttonLinkTo;

    @Inject

    String fileReference;

    @Inject

    @Named("sling:resourceType")

    String slingResourceType;

    public String getHeading() {

        return heading;

    }

    public String getButtonLabel() {

        return buttonLabel;

    }

    public String getButtonLinkTo() {

        return buttonLinkTo;

    }

    public String getSlingResourceType() {

        return slingResourceType;

    }

    public String getTitle() {

        return title;

    }

    public String getFileReference() {

        return fileReference;

    }

}

Therefore, unless you know the components and its props you want to export, this code will not work.

Highlighted

mayanky56670501

05-12-2018

So, is there any possible solution for that? or I need to write a Custom Sling Servlet only? I js wanted to know if there's any solution, otherwise the last possible solution is only to write a Custom Sling Servlet!!

Highlighted

mayanky56670501

05-12-2018

If I will make a Sling Model of Page Component, then it will only export the JSON of that page properties only. It will not work for the components lying under it.

Highlighted

smacdonald2008

05-12-2018

As you need to bind data members to component props - you cannot really use a Sling Model Exporter as shown in this article. It was really a 1-1 based on a given component. You may want to look at looking at components and reading the props you want and encoding to JSON.