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

Aem Nodes to JsonArray

Kiranchonkar
Level 2
Level 2

Hi all,

 

I am using aem 6.5

I have a requirement for converting aem page nodes to json.

Previously I used jsonItemWriter.dump method which converts data directly.

But this method is deprecated.

And second thing is in multifield we get data as item0,item1 in jsonobject format.

But my requirement is to convert this multifield to JsonArray.

 

Kindly guide for same

AEM 6.5 JSON Nodes converter
1 Accepted Solution
Rohit_Utreja
Correct answer by
Level 4
Level 4

@Kiranchonkar,

 

You can use the sling model exporter to expose the data in JSON format.

Please refer to the below URL to create the sling model export.

https://github.com/auniverseaway/sling-exporter-sample/tree/master/src/main/java/org/millr/sling

 

Now, to fetch details of multifield nodes, Please follow the below steps.

  1. Create a sling model class for the item node of multifield. E.g. if your multifield has firstName and lastName, then this sling model class(e.g. multifieldNodeModel.java)  will contain these two fields and respective getters.
  2. Create a parent sling model for the component. In this sling model, include sub sling model of the multifield node(multifieldNodeModel).

    public class componentSlingModel{
        @ChildResource
    private List<multifieldNodeModel> multifieldNodeModel;
         public List<multifieldNodeModel> getMultifieldNodeModel(){
           return multifieldNodeModel;
    }
    }


This way we can map the data of nodes to the sling model and it can be exported to the JSON format using sling model export.

Let me know if you have any questions.

View solution in original post

3 Replies
Rohit_Utreja
Correct answer by
Level 4
Level 4

@Kiranchonkar,

 

You can use the sling model exporter to expose the data in JSON format.

Please refer to the below URL to create the sling model export.

https://github.com/auniverseaway/sling-exporter-sample/tree/master/src/main/java/org/millr/sling

 

Now, to fetch details of multifield nodes, Please follow the below steps.

  1. Create a sling model class for the item node of multifield. E.g. if your multifield has firstName and lastName, then this sling model class(e.g. multifieldNodeModel.java)  will contain these two fields and respective getters.
  2. Create a parent sling model for the component. In this sling model, include sub sling model of the multifield node(multifieldNodeModel).

    public class componentSlingModel{
        @ChildResource
    private List<multifieldNodeModel> multifieldNodeModel;
         public List<multifieldNodeModel> getMultifieldNodeModel(){
           return multifieldNodeModel;
    }
    }


This way we can map the data of nodes to the sling model and it can be exported to the JSON format using sling model export.

Let me know if you have any questions.

View solution in original post

Kiranchonkar
Level 2
Level 2
Hello, thanks for replying. But problem is this is common service where i am not aware of fields of items. It can be anything. Or can have multifield inside multifield structure
Rohit_Utreja
Level 4
Level 4

Yes, As we are using @childResource annotation as mentioned above, it will consider all child nodes of multifield.

Combination of List<multifieldNodeModel> and @childResource annotation will map all item nodes of multifield to a List and then you can use it for pass in sightly or anything else.