is there any way to pass jsonString using @Exporter(name = "jackson", extensions = "json") | Community
Skip to main content
Level 2
August 18, 2023
Solved

is there any way to pass jsonString using @Exporter(name = "jackson", extensions = "json")

  • August 18, 2023
  • 4 replies
  • 892 views

package com.test.web.core.models;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import com.test.web.core.util.InsightUtil;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Model(
adaptables = Resource.class,
resourceType="test-web/components/content/secondaryNav"
)
@3484101(name = "jackson", extensions = "json")
public class SecondaryNavigationModel {

private final Logger logger = LoggerFactory.getLogger(getClass());

/**
* The Resource type.
*/
@586265
@Named("sling:resourceType")
@1497330(values = "No resourceType")
protected String resourceType;

@586265
@Optional
private String headerText;

@586265
@Optional
private String headerBackgroundColor;

@586265
@Optional
private String mobileHeaderTitle;

@586265
@Optional
private String headerLinkPath;

@586265
@Optional
private List<Resource> menuItems;

@586265
ResourceResolver resourceResolver;

private List<MenuItem> menuItemsList = new ArrayList<>();

public String getHeaderLinkPath() {
return headerLinkPath;
}

public void setHeaderLinkPath(String headerLinkPath) {
this.headerLinkPath = headerLinkPath;
}

public String getMobileHeaderTitle() {
return mobileHeaderTitle;
}

public void setMobileHeaderTitle(String mobileHeaderTitle) {
this.mobileHeaderTitle = mobileHeaderTitle;
}

public String getHeaderText() {
return headerText;
}

public void setHeaderText(String headerText) {
this.headerText = headerText;
}

public String getHeaderBackgroundColor() {
return headerBackgroundColor;
}

public void setHeaderBackgroundColor(String headerBackgroundColor) {
this.headerBackgroundColor = headerBackgroundColor;
}

public List<MenuItem> getMenuItemsList() {
return menuItemsList;
}

public void setMenuItemsList(List<MenuItem> menuItemsList) {
this.menuItemsList = menuItemsList;
}

/**
* Init.
*/
@PostConstruct
protected void init() {
// set prepared path for headerLinkPath to support EMEA
if(this.headerLinkPath != null && this.headerLinkPath.startsWith("/content/insight-web")) {
this.setHeaderLinkPath(InsightUtil.getPreparedPath(this.headerLinkPath));
}
if (menuItems != null && !menuItems.isEmpty()) {
for (Resource resource : menuItems) {
MenuItem menuItem = resource.adaptTo(MenuItem.class);
menuItemsList.add(menuItem);
}
}
}

}

 

<sly data-sly-use.templates="core/wcm/components/commons/v1/templates.html"/>
<sly data-sly-call="${templates.placeholder @ isEmpty = component.editable}"></sly>
<div data-cmp-type="react" data-sly-use.secondaryNavModel="com.test.web.core.models.SecondaryNavigationModel"
data-component="test" data-component-data=${-----?-----how to pas json}></div>

 

how to pas json in to sightly 

 

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 sherinregi-1

Hi @gmahendra 

I assume there is no direct way or method available in sightly that can consume the JSON object. In your case you are exporting the entire modal as Json and then trying to read . 

 

JSON parsing/ reading can be done with the support of Java here or if you are using any client side framework you can use the json feed directly there.

 

Please find below some links that will give you some pointers.

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/parse-json-in-sightly-js/m-p/174577

 

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

 

 

 

4 replies

sherinregi-1
Community Advisor
sherinregi-1Community AdvisorAccepted solution
Community Advisor
August 18, 2023

Hi @gmahendra 

I assume there is no direct way or method available in sightly that can consume the JSON object. In your case you are exporting the entire modal as Json and then trying to read . 

 

JSON parsing/ reading can be done with the support of Java here or if you are using any client side framework you can use the json feed directly there.

 

Please find below some links that will give you some pointers.

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/parse-json-in-sightly-js/m-p/174577

 

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

 

 

 

Preetpal_Bindra
Community Advisor
Community Advisor
August 18, 2023

Hello @gmahendra ,

From what it looks like to me, and I could be wrong in understanding this, you want to "include" the component in the other component with the selector = .model and extension = JSON.

 

thanks,

Preetpal Singh

DPrakashRaj
Community Advisor
Community Advisor
August 18, 2023

Above sling model class will only render json data when you call it as 

com.test.web.core.models.SecondaryNavigationModel.model.json

However you won't be able to parse it in json and i believe in your code you try to pass the json object on HTML DOM element to be used on frontend.

If that's the case why don't you try to call 

com.test.web.core.models.SecondaryNavigationModel.model.json

directly from front-end and parse it accordingly  

aanchal-sikka
Community Advisor
Community Advisor
August 19, 2023

Hello @gmahendra 

 

Sightly won't be able to process Json Object. 

 

Can you please try following ?

  1. Convert json into String in Sling Model
  2. Return String and consume it in sightly
Aanchal Sikka