Expand my Community achievements bar.

SOLVED

Read the Node data in json format

Avatar

Level 2

Hello,

        I am shashank singh. I am trying to read the node properties in json formate but i am getting the error message.. so is there any way to read the node property from dam path in json format.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

You can read it using google gson API.

e.g. read node properties as ValueMap and convert this object into json using gson.toJson(valueMapObj)

Example, if you are doing this in servlet

Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();

ValueMap valueMap = req.getResourceResolver().getResource("/content/we-retail/jcr:content").getValueMap();

String jsonStr = gson.toJson(valueMapObj)

Note: for jcr:data or other binary type property can't be exposed in JSON, so remove from valueMap before converting to json.



Arun Patidar

View solution in original post

3 Replies

Avatar

Level 10

Did you get a chance to go through Assets HTTP API  or Content Fragments Support in AEM Assets HTTP API since your requirements use DAM path?

You could even use custom exporter based on the complexity of your use case.

Avatar

Correct answer by
Community Advisor

Hi,

You can read it using google gson API.

e.g. read node properties as ValueMap and convert this object into json using gson.toJson(valueMapObj)

Example, if you are doing this in servlet

Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();

ValueMap valueMap = req.getResourceResolver().getResource("/content/we-retail/jcr:content").getValueMap();

String jsonStr = gson.toJson(valueMapObj)

Note: for jcr:data or other binary type property can't be exposed in JSON, so remove from valueMap before converting to json.



Arun Patidar

Avatar

Level 2

Hi Arun,

I have a use case in which AEM dialog multifield producing authoring data to be needed in a servlet for making dynamic dropdown options. Some other component which is having dropdown widget which calls the servlet using data source for dynamic dropdown. I have achieved all the requirement except one  how does servlet get to know the dynamic  path of  multifield  component are being used on the page. I also  archived this one using servlet OSGI configuration but some business reason  OSGI configuration cannot be used.

Requirement is such as.

1. multifield component responsible for storing key value pair data for dynamic dropdown.

2. Proposal is servlet needs to retrieve the multifield authoring data and it need to expose as data source for another component. 

3. Another component calls the servlet through data source.

Restriction: ACS-Commons and OSGI Config cannot be used,  

Please give you valuable thought on this.