Expand my Community achievements bar.

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

JSON Parser for Sling Servlet

Avatar

Level 2

What is the preferred library to be used for a Sling Servlet to parse a JSON data coming from a POST method? I tried to use the recommended answer based on this Stackoverflow question but I was getting an error of 405 but everything is ok when I just append the data as a request parameter like this "userName=<value>&password=<value>.

1 Accepted Solution

Avatar

Correct answer by
Level 10

 See this AEM Communty article that posts data to an AEM Sling Servlet using AJAX:

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

It uses Simple JSON Java lib to work with JSON. In this example - it creates JSON. However - you can use it to parse JSON too. 

http://www.mkyong.com/java/json-simple-example-read-and-write-json/

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

 See this AEM Communty article that posts data to an AEM Sling Servlet using AJAX:

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

It uses Simple JSON Java lib to work with JSON. In this example - it creates JSON. However - you can use it to parse JSON too. 

http://www.mkyong.com/java/json-simple-example-read-and-write-json/

Avatar

Level 9

 Does your sling Servlet has post method in it?. 

FYI : HTTP 405 status code means "method not supported".

There might be a case that you are posting data to a servlet which does not have POST method. But when you pass parameter, GET method is supported. Ideally, POST request doesn't have "?" parameter with the URL.

harold malabanan wrote...

What is the preferred library to be used for a Sling Servlet to parse a JSON data coming from a POST method? I tried to use the recommended answer based on this Stackoverflow question but I was getting an error of 405 but everything is ok when I just append the data as a request parameter like this "userName=<value>&password=<value>.

 

Avatar

Administrator

Hi 

Please have a look at these couple of community article:-

Link:- https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

// import org.json.simple.JSONObject;

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {

try
{
//Get the submitted form data that is sent from the
//CQ web page 
String id = UUID.randomUUID().toString();
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String address = request.getParameter("address");
String cat = request.getParameter("cat");
String state = request.getParameter("state");
String details = request.getParameter("details");
String date = request.getParameter("date");
String city = request.getParameter("city");

//Encode the submitted form data to JSON
JSONObject obj=new JSONObject();
obj.put("id",id);
obj.put("firstname",firstName);
obj.put("lastname",lastName);
obj.put("address",address);
obj.put("cat",cat);
obj.put("state",state);
obj.put("details",details);
obj.put("date",date);
obj.put("city",city);

//Get the JSON formatted data 
String jsonData = obj.toJSONString();

//Return the JSON formatted data
response.getWriter().write(jsonData);
}

Link:- https://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

//

<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="org.apache.sling.commons.json.io.*,org.w3c.dom.*" %><%
String filter = request.getParameter("filter");

com.adobe.cq.CustomerService cs = sling.getService(com.adobe.cq.CustomerService.class);

String XML = cs.getCustomerData(filter) ;

//Send the data back to the client
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("xml");
writer.value(XML);

writer.endObject();
%>
$.ajax(url, {
dataType: "text",
success: function(rawData, status, xhr) {
var data;
try {
data = $.parseJSON(rawData);

//Set the fields in the forum
var myXML = data.xml;

var loopIndex = 0;

//Reference the data grid, clear it, and add new records
//queried from the Adobe CQ JCR
var oTable = $('#example').dataTable();
oTable.fnClearTable(true);

//Loop through this function for each Customer element
//in the returned XML
$(myXML).find('Customer').each(function(){

var $field = $(this);
var firstName = $field.find('First').text();

var lastName = $field.find('Last').text();
var Description = $field.find('Description').text();
var Address = $field.find('Address').text(); 

//Set the new data
oTable.fnAddData( [
firstName,
lastName,
Address,
Description,]
);
});

} catch(err) {
failure(err);
}
},
error: function(xhr, status, err) {
failure(err);
}
});

I hope this would help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni