Read json file from DAM | Community
Skip to main content
July 15, 2024
Solved

Read json file from DAM

  • July 15, 2024
  • 1 reply
  • 1388 views

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.

 

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 arunpatidar

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; } }

 

1 reply

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
July 15, 2024

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