evalJSON not supported in Service Portal

Katie A
Mega Guru

Hello, We have several Client Scripts that call a Script Include to process data and send a response.

We use the evalJSON function to transform a JSON string to an object.

However, evalJSON is no longer supported in the Service Portal.

Is there a way to transform a JSON string to an object so that it can be dot-walked easily to retrieve the values?

We are using the technique here: https://fruitionpartners.eu/blog/2015/11/17/glideajax-return-multiple-values-using-json/

Script Include

var MyCustomAjax = Class.create();

MyCustomAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  helloWorld: function() {

  var obj = {};

  obj.var1 = 'Hello';

  obj.var2 = 'World';

  var json = new JSON();

  var data = json.encode(obj);//JSON formatted string

  return data;

  },

  type: 'MyCustomAjax'

});

Client Script

function onLoad() {

  var ga = new GlideAjax('MyCustomAjax');

  ga.addParam('sysparm_name', 'helloWorld');

  ga.getXML(showMessage);

}

function showMessage(response) {

  var answer = response.responseXML.documentElement.getAttribute("answer");

  g_form.addInfoMessage(answer); //JSON String

  answer = answer.evalJSON(); //Transform the JSON string to an object NO LONGER SUPPORTED!

  g_form.addInfoMessage(answer);

  g_form.addInfoMessage(answer.var1); //Display "Hello"

  g_form.addInfoMessage(answer.var2); //Display "World"

}

1 ACCEPTED SOLUTION

Katie A
Mega Guru

I have resolved this issue by using the native Javascript JSON.parse() method.



JSON.parse() - JavaScript | MDN



answer = JSON.parse(answer);


View solution in original post

4 REPLIES 4

Katie A
Mega Guru

I have resolved this issue by using the native Javascript JSON.parse() method.



JSON.parse() - JavaScript | MDN



answer = JSON.parse(answer);


Hello,



JSON.parse doesn't seem to be working on the Catalog Client Script any help on this.



Thanks


This works for me... make sure that the UI Type is set to "Both"


Thank you!


I did something like this so it will work for both the places.



var answer = response.responseXML.documentElement.getAttribute("answer"); //JSON string


try{


  answer = answer.evalJSON();


  }


catch(e){


  answer = JSON.parse(answer);


}