Placing array in an object

AbineshM
Tera Contributor

HI 

var chr = [{"Discount5":"10","FeatureToken":"[{'tokenId': 'test', 'tokenOccurence': '1','tokenValue': 11111111}, {'tokenId': 'RCY', 'tokenOccurence': '1','tokenValue': 3}]"}];


var f = chr[0].FeatureToken;
gs.info("speed" + f);


var d = {"feature": +f};
gs.info("speed" + JSON.stringify(d));

 

for the above code i am getting the following results,

AbineshM_1-1697461006105.png

 

Why am i getting a null value for variable d, Can anyone help me with this? 
need to place the array against feature.

2 ACCEPTED SOLUTIONS

Nishant12
ServiceNow Employee
ServiceNow Employee

You just have to remove + in your  variable d assignment  var d = {"feature": +f};

try below 
var chr = [{"Discount5":"10","FeatureToken":"[{'tokenId': 'test', 'tokenOccurence': '1','tokenValue': 11111111}, {'tokenId': 'RCY', 'tokenOccurence': '1','tokenValue': 3}]"}];


var f = chr[0].FeatureToken;
gs.info("speed" + f);


var d = {"feature": f};
gs.info("speed" + JSON.stringify(d));

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@AbineshM Please update your script as follow.

 

var chr = [{"Discount5":"10","FeatureToken":"[{'tokenId': 'test', 'tokenOccurence': '1','tokenValue': 11111111}, {'tokenId': 'RCY', 'tokenOccurence': '1','tokenValue': 3}]"}];


var f = chr[0].FeatureToken;
gs.info("speed" + f);


var d = {"feature": f};
gs.info("speed" + JSON.stringify(d));

Now you should be able to stringify the value if d.

View solution in original post

2 REPLIES 2

Nishant12
ServiceNow Employee
ServiceNow Employee

You just have to remove + in your  variable d assignment  var d = {"feature": +f};

try below 
var chr = [{"Discount5":"10","FeatureToken":"[{'tokenId': 'test', 'tokenOccurence': '1','tokenValue': 11111111}, {'tokenId': 'RCY', 'tokenOccurence': '1','tokenValue': 3}]"}];


var f = chr[0].FeatureToken;
gs.info("speed" + f);


var d = {"feature": f};
gs.info("speed" + JSON.stringify(d));

Sandeep Rajput
Tera Patron
Tera Patron

@AbineshM Please update your script as follow.

 

var chr = [{"Discount5":"10","FeatureToken":"[{'tokenId': 'test', 'tokenOccurence': '1','tokenValue': 11111111}, {'tokenId': 'RCY', 'tokenOccurence': '1','tokenValue': 3}]"}];


var f = chr[0].FeatureToken;
gs.info("speed" + f);


var d = {"feature": f};
gs.info("speed" + JSON.stringify(d));

Now you should be able to stringify the value if d.