unable to access JSONObject in sightly html file | Community
Skip to main content
Level 3
Jan 20, 2016 10:29 AM UTC
Solved

unable to access JSONObject in sightly html file

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.
swathiv54399501Accepted solution
Level 3
Jan 22, 2016 10:37 AM UTC

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
Level 2
May 31, 2017 1:34 PM UTC

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

Translate
smacdonald2008
Level 10
May 31, 2017 1:16 PM UTC

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…

Translate
teufeld
Level 2
May 31, 2017 1:08 PM UTC

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.

Translate
Mani_kumar_
New Participant
Jan 22, 2016 1:29 PM UTC

Thanks for sharing your solution 

Translate
GK-007
Level 9
Jan 22, 2016 12:45 PM UTC

Thanks for sharing it.

Helpful!!!

Translate
swathiv54399501AuthorAccepted solution
Level 3
Jan 22, 2016 10:37 AM UTC

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

Translate
Kunal_Gaba_
New Participant
Jan 20, 2016 10:23 PM UTC
Jitendra_S_Toma
Level 10
Jan 20, 2016 4:39 PM UTC

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

for instance 

${jsonObject @ context='unsafe'}

Jitendra

Translate
Level 3
Jan 20, 2016 3:54 PM UTC

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

Translate
Jitendra_S_Toma
Level 10
Jan 20, 2016 3:42 PM UTC

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

Translate