unable to access JSONObject in sightly html file | Community
Skip to main content
New Participant
January 20, 2016
Solved

unable to access JSONObject in sightly html file

  • January 20, 2016
  • 17 replies
  • 11668 views

Hi,

I am trying to access JSONObject  in sightly html,but the data is not displayed.

Below is the sightly java file,where "result" variable is holding JSON string.

public class TestSightly extends WCMUse {

   public String result;
   public JSONObject jsonObject;   
    
    @Override
    public void activate() throws Exception {
        TestLocation testLocation = new TestLocation();
        result = testLocation.testMessage();
        jsonObject = new JSONObject(result);
        
    }

     public String getResult(){
        return result;
    }
    
    public JSONObject getJsonObject(){
        return jsonObject
    }
       
}

HTML File:

<div data-sly-include="/libs/wcm/core/components/init/init.jsp"></div>

<div data-sly-use.model="${'com.test.core.models.TestSightly'}">
 <div>${model.result}</div>
 <div>${model.jsonObject}</div>

</div>

 

With the above "model.result" is displaying the string data(json formatted data),but "model.jsonObject" is displaying nothing.

 

Thanks & Regards

swati

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by swathiv54399501

Issue fixed.Found alternate way.

Instead of using org.apache.sling.commons.json.JSONObject API, I changed to GSON api and converted json to javabean object and then accessing in sightly file.

But using JSONObject in sightly is still not possible.

swati

17 replies

teufeld
New Participant
May 31, 2017

I agree with you and it's one of many reason I do JAVA and not .Net, because you can use and mix different API.

I just start with sightly instead of doing everything with a JSP, and was a litlle upset to not understand why an API from Google works better than an API from Apache in AEM

smacdonald2008
New Participant
May 31, 2017

that is the nice thing about AEM - you are not bound to a specific API. Its essentially a JAVA platform, so you can use a specific Java API that will solve your business requirements.

A rule is the Java API must be in a OSGi bundle and in an active state so other bundles can use that API.

We have an article that shows use of the GSON API in a Java backend:

Scott's Digital Community: Creating an AEM HTML Template Language Component that displays data from a Restful Web Servic…

teufeld
New Participant
May 31, 2017

I got the same issue, I spent one day to understand why I'm unable to get the value of a JsonObject in sightly (only key) if the JsonObject come a java code with org.apache.sling.commons.json.JSONObject;

At the end I end up with another solution than the use of Gson api.

I used a JS conversion between java and sightly. I gave my Java JsonObject to a Javascript method JSON.parse()

something like:

html

<sly data-sly-use.greatdeals="GreatDeals" />

    <sly data-sly-use.greatdealsJS="${'greatdeals.js' @ jsonString=greatdeals.jsonGreatDeals}" />

<sly data-sly-list.deal="${greatdealsJS}">

    <div>${deal} : ${greatdealsJS[deal]}</div>

</sly>

greatdeal.js:

use(function () {

    return JSON.parse(this.jsonString);

});

GreatDeals

public JSONObject getJsonGreatDeals() {

        JSONObject jsonObject = null;

        try {

            jsonObject = new JSONObject();

            jsonObject.put("deal1", "test1");

            jsonObject.put("deal2", "test2");

            jsonObject.put("deal3", "test3");

        } catch (Exception e) {

            LOGGER.error("Could not create JSON", e);

        }

        return jsonObject;

    }

it's not beautiful but it works.

Mani_kumar_
New Participant
January 22, 2016

Thanks for sharing your solution 

GK-007
New Participant
January 22, 2016

Thanks for sharing it.

Helpful!!!

swathiv54399501AuthorAccepted solution
New Participant
January 22, 2016

Issue fixed.Found alternate way.

Instead of using org.apache.sling.commons.json.JSONObject API, I changed to GSON api and converted json to javabean object and then accessing in sightly file.

But using JSONObject in sightly is still not possible.

swati

Kunal_Gaba_
New Participant
January 20, 2016
Jitendra_S_Toma
New Participant
January 20, 2016

Yes. I had the same feeling. Try using context="unsafe" while rendering json object. Just try that.

for instance 

${jsonObject @ context='unsafe'}

Jitendra

New Participant
January 20, 2016

I already tried with jsp, it is working fine.When I tried to convert that jsp to sightly html file,I faced this issue.

Jitendra_S_Toma
New Participant
January 20, 2016

Swathi,

I think the issue is not with the Sightly POJO file. Do you mind testing same thing with JSP?.  It is just a guess that something strange happening because of Sightly.

Jitendra