Is there any possibility to read the json object in sightly | Community
Skip to main content
June 24, 2022
Solved

Is there any possibility to read the json object in sightly

  • June 24, 2022
  • 5 replies
  • 2335 views

From sling Model I'm returning the list of json objects like the below
[{"day":"Monday","from":"10:00","to":"21:00"},{"day":"Tuesday","from":"10:00","to":"21:00"}]
Instead of pojo I want to read the data in sightly.

<sly data-sly-list.workingHours="${item.workingDays}">
<p>${workingHours['day']}:from ${workingHours.from} to ${workingHours.to}</p>
</sly>

 Tried with square notation also none of them worked any suggestion please.

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 joerghoh

If you already have a sling model, why don't you instantiate it in your HTL script and read the requested data from it?

5 replies

SantoshSai
Community Advisor
Community Advisor
June 24, 2022

Hi @kiran6-2 ,

There's no explicit API to parse JSON directly in HTL. 

  • If you are using JS-Use API then its ultimately JS at the end and parsing it is nothing but simply using the keys to get desired value out of JSON.
  • If you are using Java-Use API then the one I have been using to map JSON to java objects is GSON. GSON is pretty handy in converting JSON to JAVA Object and vice-versa.

Instead of using org.apache.sling.commons.json.JSONObject API, change to GSON API and convert JSON to Java bean object and then access in HTL.
Sharing piece of code for your reference which was already discussed:

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);
});

GreatDeal.java

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;
    }

 Reference: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/unable-to-access-jsonobject-in-sightly-html-file/m-p/178852

Hope that helps you!

Regards,

Santosh

Santosh Sai
ShaileshBassi
Community Advisor
Community Advisor
June 25, 2022

@kiran6-2 There is an alternate way:

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

 

But using JSONObject in sightly is still not possible.

 

Thanks

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
June 25, 2022

If you already have a sling model, why don't you instantiate it in your HTL script and read the requested data from it?

sunil_kumar_
Level 5
June 25, 2022

Hi @kiran6-2 As you are already using Sling Model and returning List. Iterate JSON in sling model and put each iteration in either Map to Bean. Keep all these objects in List. Iterate this list in Sightly. 

/*  Write your code to add information in Map and List */    
public List<Map<String,String>> getWorkingDays(){
        List<Map<String,String>> days=new ArrayList<>();
        Map<String,String> day1=new HashMap<>();
        day1.put("day","Monday");day1.put("from","9:00");day1.put("to","4:00");
        days.add(day1);
        Map<String,String> day2=new HashMap<>();
        day2.put("day","Tuesday");day2.put("from","10:00");day2.put("to","5:00");
        days.add(day2);
        return days;
    }

Iterate in Sightly

/*  htl is sling model object */
<div data-sly-list.item="${htl.workingDays}">
   <p> Day : ${item.day} -> From  ${item.from} to ${item.to}</p>
</div>
ShaileshBassi
Community Advisor
Community Advisor
June 29, 2022

@kiran6-2 Use Handlebars, if you do not want the component to be cached and are getting the json response via the AJAX request.
There's no explicit api to parse json directly in sightly. Instead you can dump the json in a handlebar template which can display the json data.

 

If you are using sightly, then as suggested by @joerghoh instantiate it in your HTL script and read the requested data from it, or 

Parse the json in the SlingModel and either return a List or a Map and iterate the same in sightly.

Thanks