Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

How to use content fragment as an API response?

Avatar

Level 5

Hi All,

I have a json object and I want to create an API in AEM using content fragment. In content fragment I want to put complete JSON object and expose the JSON object and JSON properties using parameterized API. Could someone share some idea how we can achieve this and make a content fragment as a API? Thank you in advance.

 

Regards

Rashid

 

9 Replies

Avatar

Community Advisor

Hi @RashidJorvee ,

 

I am just trying to understand. You already have a JSON Object and I guess it can be directly exposed through Servlet. May I know why you need content fragment to put that JSON Object ?

Avatar

Level 5

I want to make it configurable, so that author can edit the json properties. Below is a scenario.

E.g. JSON

{
"14": { "100": 12, "75" : "18", "50": "32"},
"12": { "100": 12, "75" : "18", "50": "32"}
}

 

And with above JSON object, trying to retrieve JSON object based on matching value 14 or 12. Something where I pass a parameter with CF endpoint and it return one matching json object.

Request: localhost/content/rashid/site/jorvee/model.json?class=12 and in return I receive

Response: { "100": 12, "75" : "18", "50": "32"}

 

Avatar

Community Advisor

HI @RashidJorvee 

 

You can create a content fragment model and add 'JSON Object' field which accepts json objects. Refer this.

dev_aem_1-1644167241637.png

Then go ahead and create cfs using this cf model.

The content fragment data can be accessed over ContentFragment API(com.adobe.cq.dam.cfm.ContentFragment). Refer this link. Hope this helps.

 

Thanks

Avatar

Level 5

Thank you @JeevanRaj, I used this JSON object field but it is not providing expected result. Let me look and try CF API.

Avatar

Community Advisor

@RashidJorvee ,

 

Not sure what didnt work for you but i was able to get what you were looking for. Below are the steps that i followed.

  1. Create a cf model with JSON Object field. 
  2. Create cf using this cf model and provide the json as input to the field.
    JSON input: {
    "14": { "100": 14, "75" : "18", "50": "32"},
    "12": { "100": 12, "75" : "18", "50": "32"}
    }
    dev_aem_0-1644170392566.png
  3. Created a sling servlet. Pass the parameter to this sling servlet. Read the cf json data in the sling servlet using the CF API. Once you have the json data,  look for the key passed via paramater.
    Request: http://localhost:4504/bin/testjson?param=14
    Response: 
    {"100":14,"75":"18","50":"32"}
    dev_aem_1-1644170847136.png

     

    Below is a working sample of this sling servlet.
    @Component(service = Servlet.class, property = {
    Constants.SERVICE_DESCRIPTION + "=Test JSON from CF servlet",
    "sling.servlet.paths=" + "/bin/testjson",
    "sling.servlet.methods=" + HttpConstants.METHOD_GET
    })
    public class TestJSONServlet extends SlingSafeMethodsServlet {

    @Reference
    private ResourceResolverUtil resolverUtil;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    String param = request.getParameter("param");
    try(ResourceResolver rr = resolverUtil.getResourceResolver()) {
    ContentFragment cf = rr.getResource("/content/dam/we-retail/en/faqs/company/testjson").adaptTo(ContentFragment.class);
    if(cf.hasElement("json")){
    ContentElement json = cf.getElement("json");
    JsonObject jsonObject = new Gson().fromJson(cf.getElement("json").getContent(), JsonObject.class);
    response.getWriter().print(jsonObject.get(param).toString());
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    Thanks

Avatar

Community Advisor

Posted a reply but not seeing it after page load. Weird. So posting it again.

 

@RashidJorvee ,

 

Not sure what didn't work for you but below solution worked for me. Below are the steps that i followed, hope the same works for you.

  1. Create a cf model with JSON Object field.
  2. Create cf using this cf model. Provide the json as input to this field.
    JSON input: {
    "14": { "100": 14, "75" : "18", "50": "32"},
    "12": { "100": 12, "75" : "18", "50": "32"}
    }
    dev_aem_0-1644171288782.png
  3. Create a sling servlet with accepts a parameter. Read the cf data into this sling servlet using the CF API. Look through the json to find the key passed into the servlet as param.
    Request: http://localhost:4504/bin/testjson?param=14
    Response: 
    {"100":14,"75":"18","50":"32"}
    dev_aem_1-1644171412780.png

    Below is the working sample of the servlet.

    @Component(service = Servlet.class, property = {
    Constants.SERVICE_DESCRIPTION + "=Test JSON from CF servlet",
    "sling.servlet.paths=" + "/bin/testjson",
    "sling.servlet.methods=" + HttpConstants.METHOD_GET
    })
    public class TestJSONServlet extends SlingSafeMethodsServlet {

    @Reference
    private ResourceResolverUtil resolverUtil;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    String param = request.getParameter("param");
    try(ResourceResolver rr = resolverUtil.getResourceResolver()) {
    ContentFragment cf = rr.getResource("/content/dam/we-retail/en/faqs/company/testjson").adaptTo(ContentFragment.class);
    if(cf.hasElement("json")){
    ContentElement json = cf.getElement("json");
    JsonObject jsonObject = new Gson().fromJson(cf.getElement("json").getContent(), JsonObject.class);
    response.getWriter().print(jsonObject.get(param).toString());
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    Thanks



Avatar

Community Advisor

Posted a reply twice but it is disappearing immediately after page reload. Weird. So posting it again.

 

@RashidJorvee ,

 

Not sure what didn't work for you but below solution worked for me. Below are the steps that i followed, hope the same works for you.

  1. Create a cf model with JSON Object field.
  2. Create cf using this cf model. Provide the json as input to this field.
    JSON input: {
    "14": { "100": 14, "75" : "18", "50": "32"},
    "12": { "100": 12, "75" : "18", "50": "32"}
    }
  3. Create a sling servlet with accepts a parameter. Read the cf data into this sling servlet using the CF API. Look through the json to find the key passed into the servlet as param.
    Request: http://localhost:4504/bin/testjson?param=14
    Response: {"100":14,"75":"18","50":"32"}
    dev_aem_0-1644171707651.png

     

Below is the working sample of the servlet.

@Component(service = Servlet.class, property = {
Constants.SERVICE_DESCRIPTION + "=Test JSON from CF servlet",
"sling.servlet.paths=" + "/bin/testjson",
"sling.servlet.methods=" + HttpConstants.METHOD_GET
})
public class TestJSONServlet extends SlingSafeMethodsServlet {

@Reference
private ResourceResolverUtil resolverUtil;

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
String param = request.getParameter("param");
try(ResourceResolver rr = resolverUtil.getResourceResolver()) {
ContentFragment cf = rr.getResource("/content/dam/we-retail/en/faqs/company/testjson").adaptTo(ContentFragment.class);
if(cf.hasElement("json")){
ContentElement json = cf.getElement("json");
JsonObject jsonObject = new Gson().fromJson(cf.getElement("json").getContent(), JsonObject.class);
response.getWriter().print(jsonObject.get(param).toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


Thanks

 

Avatar

Level 5

Thank you @JeevanRaj this will be helpful.

I was thinking something without Servlet, just using CF.