Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Using JSONObject in Sightly ?

Avatar

Level 2

Is there a way to use org.apache.sling.commons.json.JSONObject on Sightly ?

For example, if my Java class exposes a JSONObject :

public class Profile extends WCMUse { public JSONObject profileJson; public Profile() { super(); } @Override public void activate() throws Exception { /*  { "user": { "surname": "Paul", "job": "developer" }, "age": 28 } */ String profileJsonString = "{\"user\":{\"surname\":\"Paul\",\"job\":\"developer\"},\"age\":28}"; profileJson = new JSONObject(profileJsonString); } public JSONObject getProfileJson(){ return profileJson; } }

Is there a way to access JSON fields on html files like that :

<div data-sly-unwrap data-sly-use.profile="${'com.test.Profile'}"></div> <h1>Profile</h1> <p>Surname : ${profile.profileJson.user.surname}</p> <p>Age : ${profile.profileJson.age}</p>
1 Accepted Solution

Avatar

Correct answer by
Level 10

Just spoke to Eng about this. This is not possible with Slighty. Why not use JavaScript and AJAX. Perform a GET on a Servlet - that returns JSON. Parse the JSON via JavaScript. See: 

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

Just spoke to Eng about this. This is not possible with Slighty. Why not use JavaScript and AJAX. Perform a GET on a Servlet - that returns JSON. Parse the JSON via JavaScript. See: 

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html