Json Object

wcf:json  Tag 

The wcf:json tag converts a specified Java object to JavaScript Object Notation (JSON).If the specified object is a Java boolean, number, string, date, character, class, or throwable class object, the tag json converts the object to a string by calling the toString() method of the tag. The resulting object is URL encoded. If the object is an SDO data object, the object is parsed and a structured JSON document is created.


The wcf:json tag operates on Java objects, not Java primitives.It cannot convert objects that reference themselves. That is, if an object references itself, a stack overflow error occurs.I have taken the example of AjaxActionErrorResponse.jsp which will create  the error response in the case of an Ajax service call.The following  example converts  the (errorBean.errorCode, errorMessage, errorBean.messageKey, errorBean.messageParam)   to Json notation  as shown below.

<wcbase:useBean id="errorBean" classname="com.ibm.commerce.beans.ErrorDataBean"/>
 <c:set var="errorMessage" value="${errorBean.message}"/>

"errorCode": <wcf:json object="${errorBean.errorCode}"/>,
"errorMessage": <wcf:json object="${errorMessage}"/>,
"errorMessageKey": <wcf:json object="${errorBean.messageKey}"/>,
"errorMessageParam": <wcf:json object="${errorBean.messageParam}"/>

And in  AJAX call  (client side)see the failure Handler  method   where the JSON response  is  filtered as below.

id: "AjaxAddOrderItem",
actionId: "AjaxAddOrderItem",
url: getAbsoluteURL() + "AjaxOrderChangeServiceItemAdd",
formId: ""
,successHandler: function(serviceResponse) {
,failureHandler: function(serviceResponse) {
alert (serviceResponse.errorMessageKey );

JSON.parse() vs eval()

JSON.parse (text) is used to convert a JSON text into a JavaScript object.Browsers without the support for the JavaScript function JSON.parse () can use the eval() function to convert a JSON text into a JavaScript object.

var value = eval ("(" + text + ")");

json.parse() just parses JSON whereas eval is open to all javascript expressions.All browsers don’t have native JSON support so there will be times where we need to use eval() to the JSON string.The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted.It is much safer to use a JSON parser.

Please see the below example for more details.

var temp = "(function()
/* insert code which affect security */
return {a: 1, b: 2,c:3};

var value  = eval(temp);
variable  value  will get  the value as  {a: 1,b:2,c:3}  but this  will also execute the code which affect security.But in the case of JSON.parse()will throw exception as it expects the value to be proper Json.
var value = JSON.parse(temp);
The above code will throw  exception Uncaught Syntax Error: Unexpected token.

1 comment:

  1. Hi,
    I am struggling with the problem, that on Websphere Commerce 7 the responses sent by REST are truncated, so on the remote client I am unable to parse JSON into Java POJO-s. The responses's size vary a lot, and it can be quite huge. The Rest service has proven very unreliable. Is there any settings in Websphere server, to enable large data outputs?