Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

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

Avatar

Level 2

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

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

10 Replies

Avatar

Community Advisor

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

 

 

 

 

 

Avatar

Correct answer by
Community Advisor

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

Avatar

Level 2

@A_H_M_Imrul 

 

not working...where exactly do i need to put the resource folder?

Avatar

Community Advisor
- ui.core
    - src
      - main
        - java
        - resources   <- here is the folder
      - test
    - pom.xml
- ui.apps

Avatar

Level 2

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

 

Avatar

Community Advisor

What are you getting in the "model" in the sightly above? is that a list of Product objects? 

Avatar

Level 2

Heyy @A_H_M_Imrul it is working as expected. Thank you very much.