Component that produces JSON
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!
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!
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:
//
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
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.