Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

Read json file from DAM

Avatar

Level 8

Hello Team,

 

I wanted to read json file from DAM location in my sling model. Can, someone help me with a valid, performance efficient logic?

 

Thanks in advance.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Mahesh_Gunaje 

Try with this ChatGpt generated code snippet, please test and validate this before you use this for production

 

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Optional;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.DamConstants;
import org.apache.commons.io.IOUtils;
import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class JsonFromDamModel {

    @Self
    private Resource resource;

    @ScriptVariable
    private ResourceResolver resourceResolver;

    private JsonObject jsonObject;

    @PostConstruct
    protected void init() {
        String damPath = "/content/dam/path/to/your/jsonfile.json"; // Path to your JSON file in DAM
        Resource jsonResource = resourceResolver.getResource(damPath);
        
        if (jsonResource != null && jsonResource.isResourceType(DamConstants.NT_DAM_ASSET)) {
            Asset asset = jsonResource.adaptTo(Asset.class);
            if (asset != null) {
                Resource original = asset.getOriginal();
                if (original != null) {
                    try (InputStream is = original.adaptTo(InputStream.class)) {
                        if (is != null) {
                            String jsonContent = IOUtils.toString(is, StandardCharsets.UTF_8);
                            jsonObject = JsonParser.parseString(jsonContent).getAsJsonObject();
                        }
                    } catch (Exception e) {
                        // Handle the exception appropriately
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public JsonObject getJsonObject() {
        return jsonObject;
    }
}

 



Arun Patidar

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @Mahesh_Gunaje 

Try with this ChatGpt generated code snippet, please test and validate this before you use this for production

 

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Optional;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.DamConstants;
import org.apache.commons.io.IOUtils;
import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class JsonFromDamModel {

    @Self
    private Resource resource;

    @ScriptVariable
    private ResourceResolver resourceResolver;

    private JsonObject jsonObject;

    @PostConstruct
    protected void init() {
        String damPath = "/content/dam/path/to/your/jsonfile.json"; // Path to your JSON file in DAM
        Resource jsonResource = resourceResolver.getResource(damPath);
        
        if (jsonResource != null && jsonResource.isResourceType(DamConstants.NT_DAM_ASSET)) {
            Asset asset = jsonResource.adaptTo(Asset.class);
            if (asset != null) {
                Resource original = asset.getOriginal();
                if (original != null) {
                    try (InputStream is = original.adaptTo(InputStream.class)) {
                        if (is != null) {
                            String jsonContent = IOUtils.toString(is, StandardCharsets.UTF_8);
                            jsonObject = JsonParser.parseString(jsonContent).getAsJsonObject();
                        }
                    } catch (Exception e) {
                        // Handle the exception appropriately
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public JsonObject getJsonObject() {
        return jsonObject;
    }
}

 



Arun Patidar