Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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"
)
@exporter(name = "jackson", extensions = "json")
public class SecondaryNavigationModel {

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

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

@inject
@Optional
private String headerText;

@inject
@Optional
private String headerBackgroundColor;

@inject
@Optional
private String mobileHeaderTitle;

@inject
@Optional
private String headerLinkPath;

@inject
@Optional
private List<Resource> menuItems;

@inject
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 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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...

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/unable-to-access-jsonobjec...

 

 

 

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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...

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/unable-to-access-jsonobjec...

 

 

 

Avatar

Community Advisor

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

Avatar

Community Advisor

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  

Avatar

Community Advisor

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