Component that produces JSON | Community
Skip to main content
Level 2
January 27, 2017
Solved

Component that produces JSON

  • January 27, 2017
  • 2 replies
  • 2458 views

Hi,

Is it possible to create an AEM component that will generate a JSON file instead of an HTML? If that's possible, could you point me to some online resources to see how it's done.

Thanks!

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 kautuk_sahni

Yes, we can do it.

Please have a look at this article :

Link:- http://www.nateyolles.com/blog/2015/12/converting-aem-sling-resources-to-json

// Converting AEM/Sling Resources to JSON : You can easily convert an AEM Page, Sling Resource, or JCR Node to JSON using the org.apache.sling.commons.json.jcr.JsonItemWriter class. 

 

Also have a look at this example:

Link:- https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/adobe/acs/samples/servlets/impl/SampleAllMethodsServlet.java

//

     JSONObject jsonResponse = new JSONObject();
            try {
                jsonResponse.put("success", true);
                jsonResponse.put("new-world", newWorld);
                // Write the JSON to the response
                response.getWriter().write(jsonResponse.toString(2));
                // Be default, a 200 HTTP Response Status code is used
            } catch (JSONException e) {
                log.error("Could not formulate JSON response");
                // Servlet failures should always return an approriate HTTP Status code
                response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                // If you do not set your own HTML Response content, the OOTB HATEOS Response is used
                response.getWriter().write("ERROR");
            }
        }

I hope this would help you.

~kautuk

2 replies

smacdonald2008
Level 10
January 27, 2017

AEM provides information in JSON by default: 

https://docs.adobe.com/docs/en/aem/6-2/develop/components/pageinfo.html

If you want to create a custom component - you can build an OSGi service that can encode data to JSON or a Sling Servlet that returns JSON. 

kautuk_sahni
Community Manager
kautuk_sahniCommunity ManagerAccepted solution
Community Manager
January 30, 2017

Yes, we can do it.

Please have a look at this article :

Link:- http://www.nateyolles.com/blog/2015/12/converting-aem-sling-resources-to-json

// Converting AEM/Sling Resources to JSON : You can easily convert an AEM Page, Sling Resource, or JCR Node to JSON using the org.apache.sling.commons.json.jcr.JsonItemWriter class. 

 

Also have a look at this example:

Link:- https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/adobe/acs/samples/servlets/impl/SampleAllMethodsServlet.java

//

     JSONObject jsonResponse = new JSONObject();
            try {
                jsonResponse.put("success", true);
                jsonResponse.put("new-world", newWorld);
                // Write the JSON to the response
                response.getWriter().write(jsonResponse.toString(2));
                // Be default, a 200 HTTP Response Status code is used
            } catch (JSONException e) {
                log.error("Could not formulate JSON response");
                // Servlet failures should always return an approriate HTTP Status code
                response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                // If you do not set your own HTML Response content, the OOTB HATEOS Response is used
                response.getWriter().write("ERROR");
            }
        }

I hope this would help you.

~kautuk

Kautuk Sahni