The CreatorCon Call for Content is officially open! Get started here.

Need help to pass values in a JSON object

avi2329
Tera Contributor

Hello All,

 

I am trying to dynamically pass few value (which are fields on the form) into the JSON object which is as below:

var obj = {"name": "value1", "image": "value2"};

The value1 and value2 are the field values on an incident and i have a requirement where i need to pass those values.

Ex: where value1 = current.short_description && value2 == current.description which i need to pass on to obj variable. I have tried many different ways but couldn't be successful. Can some one help me out on this?

       

3 REPLIES 3

HarshTimes
Tera Guru

Use below

var strObj = new JSON();
strObj.description = current.description;
strObj.short_description = current.short_description;

 

Pass the strObj to script where you want to use. For more details about the JSON read the ServiceNow doc link.

https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_JSON-decode_S

 

 

-harsh

HarshTimes
Tera Guru

YOu can also try something below.

var json = new JSON();
var Obj = {
	"short_description" : current.short_description,
	"description" : current.description
	};
var data = json.encode(Obj);
return data;

Shrutika Surwad
Kilo Guru
this is trial example: 

var str = ‘{“name":"George","lastname":"Washington”}’;
var obj = JSON.parse(str);
gs.info(‘The first name is ’  + obj.name);