JSON Object - How to create?

rebecca75
Tera Contributor

I have a field on the Task table called 'u_input_parms' that will be used in orchestration. The idea is to have this field as a JSON Object

that I'll use to pass values into my workflow/orchestration.

Currently, the 'u_input_parms' field will have 'workflow name' to fire, input_parm1 and input_parm2.

My question, how do I create this field as a JSON object so that I can call 'Input_parm1' etc? We are on Helsinki and my understanding

is that the json function isn't available until a later version.

11 REPLIES 11

Okay,



Here is what I have, but I'm getting nothing for input1 and 2 and there are values in these variables. Ideas?



if (uatm.u_orch_name_add != '' && uatm.u_orch_name_add != undefined) {


var obj = {"input1": uatm.u_input_parm_1_add.u_input_parm_1,"input2": uatm.u_input_parm_2_add};


var parser = new JSON();


var str = parser.encode(obj);



//test


//var obj = parser.decode(str);


gs.log('The object '   + str);



The object {"input1":{},"input2":{}}


vab_13
ServiceNow Employee
ServiceNow Employee

JSON.stringify() - JavaScript | MDN


and JSON().decode



i,.e:



var str   = {"type":"myType","description":"myDescription","message":"{\"url\":\"http://www.something.com\"}"}  


var obj = new JSON().decode(str);  


gs.log(str['message']);  



Will print:



[0:00:00.001] Script completed in scope global: script



*** Script: {"url":"http://www.something.com"}

rebecca75
Tera Contributor

I'm not sure how to get the variables to show data in the JSON string. I can't put quotes around the variables, because then the text of the variable name comes through to the JSON string. Ideas on how to get the actual value in the JSON string and not the name of the variable?



if (uatm.u_orch_name_add != '' && uatm.u_orch_name_add != undefined) {


var obj = {"input1": uatm.u_input_parm_1_add.u_input_parm_1,"input2": uatm.u_input_parm_2_add};


var parser = new JSON();


var str = parser.encode(obj);



//test


//var obj = parser.decode(str);


gs.log('The object '   + str);



The object {"input1":{},"input2":{}}


You would have to put it in a string with mixed single quote and double quotes, like:



'{"input1":' + uatm.u_input_parm_1_add.u_input_parm_1 + ',"input2":' + uatm.u_input_parm_2_add + '}';


Thank you -



I have this now:



if (uatm.u_orch_name_add != '' && uatm.u_orch_name_add != undefined) {


var obj = '{"input1":' + uatm.u_input_parm_1_add.u_input_parm_1 + ',"input2":' + uatm.u_input_parm_2_add + '}';


var parser = new JSON();


var inputparms = parser.encode(obj);



gs.log('The object ' + inputparms);



                            The object "{\"input1\":AANurseGroup,\"input2\":nUCMiU9ApkSQRMx8TMo5RA==}"



When I use the following to decode, I get nada...



//test


var str = parser.decode(inputparms);


gs.log(str['input2']);



                            undefined



Ideas on how to decode and call input2?