JSON Parser for Sling Servlet | Community
Skip to main content
New Participant
February 26, 2016
Solved

JSON Parser for Sling Servlet

  • February 26, 2016
  • 3 replies
  • 8238 views

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>.

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 smacdonald2008

 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/

3 replies

kautuk_sahni
Employee
February 26, 2016

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
Jitendra_S_Toma
New Participant
February 26, 2016

 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>.

 

smacdonald2008
smacdonald2008Accepted solution
New Participant
February 26, 2016

 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/