How to read a JSON file and map it to the sling model | Community
Skip to main content
Level 2
September 30, 2023
Solved

How to read a JSON file and map it to the sling model

  • September 30, 2023
  • 1 reply
  • 4558 views

I am having a JSON model locally and I want that JSON data into my sling model and upon request to that sling model it should send the whole JSON data to my sightly. But I am unable to get the data in the sightly.

 

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ProductsModel { static final Logger log = LoggerFactory.getLogger(ProductsModel.class); public List<Product> getProducts() { ObjectMapper objectMapper = new ObjectMapper(); File jsonFile = new File("absolute path of the json file"); List<Product> products = null; try { products = objectMapper.readerForListOf(Product.class).readValue(jsonFile); } catch (IOException e) { log.info(e.getMessage()); } return products; } }

 

Here is my Product class

 

class Product { @JsonProperty("image") private String image; @JsonProperty("title") private String title; @JsonProperty("description") private String description; public String getImage() { return image; } public String getTitle() { return title; } public String getDescription() { return description; } }

 

And here is my HTL code to get the products

 

<div data-sly-use.model="com.adobe.aem.core.models.ProductsModel"> <div data-sly-list.products="${model.products}"> ${products.title} </div> </div>

 

 

So my output is just blank, not showing anything...please help...
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 A_H_M_Imrul

did you get the json into the File object? If the about solution doesn't work you can try the following as well

 

import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.JsonNode; import org.osgi.framework.FrameworkUtil; // Assuming you have the ObjectMapper instance ready. ObjectMapper objectMapper = new ObjectMapper(); // Load the JSON file directly into a JsonNode JsonNode jsonNode = objectMapper.readTree(FrameworkUtil.getBundle(ProductsModel.class) .getBundleContext() .getBundle() .getResource("somedata.json")); // Use Jackson's TypeFactory to create a collection type // org.osgi.framework.FrameworkUtil; TypeFactory typeFactory = objectMapper.getTypeFactory(); CollectionType productListType = typeFactory.constructCollectionType(List.class, Product.class); // Deserialize the JsonNode into a list of Product objects List<Product> productObjectList = objectMapper.convertValue(jsonNode, productListType);

 

 See if this one works..

1 reply

A_H_M_Imrul
Community Advisor
Community Advisor
September 30, 2023

@khasha 

 

You can try this: putting the file in the resources folder 

 

File file = new File( this.getClass().getClassLoader().getResource("someName.json").getFile() );

 

then you can map the json file into the Object:

 

ObjectMapper mapper = new ObjectMapper(); SomeClass someClassObj = mapper.readValue(file, SomeClass.class);

 

 

 

 

 

khaSHAAuthor
Level 2
September 30, 2023

@a_h_m_imrul 

 

I am having the array of items in my json

khaSHAAuthor
Level 2
September 30, 2023

did you get the json into the File object? If the about solution doesn't work you can try the following as well

 

import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.JsonNode; import org.osgi.framework.FrameworkUtil; // Assuming you have the ObjectMapper instance ready. ObjectMapper objectMapper = new ObjectMapper(); // Load the JSON file directly into a JsonNode JsonNode jsonNode = objectMapper.readTree(FrameworkUtil.getBundle(ProductsModel.class) .getBundleContext() .getBundle() .getResource("somedata.json")); // Use Jackson's TypeFactory to create a collection type // org.osgi.framework.FrameworkUtil; TypeFactory typeFactory = objectMapper.getTypeFactory(); CollectionType productListType = typeFactory.constructCollectionType(List.class, Product.class); // Deserialize the JsonNode into a list of Product objects List<Product> productObjectList = objectMapper.convertValue(jsonNode, productListType);

 

 See if this one works..


@a_h_m_imrul 

 

this is working fine and even while returning i am able to see the log printing the values, but in the HTL while rendering it I am not able to see the value.

<div data-sly-use.model="com.adobe.aem.core.models.ProductsModel"> <div data-sly-list.products="${model.products}"> ${products.title} </div> </div>