dynamic json script to store variable values

nidhi21
Kilo Explorer

Hi, I have a requirement where i need to store variable values as json object but it need to be dynamic, as in the variables are different for every record hence it should dynamically use the variable name as object name and the corresponding value. I am using this in the workflows so that i can get and store variable values for different catalog items.Can someone help me with the script?

8 REPLIES 8

Kalaiarasan Pus
Giga Sage

JSON object also supports bracket notation along with '.' access notation similar to array. So you can do something like

 

item = {}
item ["title"] = id;
item ["email"] = email;

The key value in the square bracket can be dynamic via your script

Hi,

actually the same workflow would be used for different catalog items , so the script need to store variable values for whichever item orderd as json and input those values as string in incident task description. hence the script needs to be generic and used for multiple diffrernt variables.

The key name inside square brackets can be dynamic. You just need to script it the way you want.

 

Sample:

var sampleKey = 'title';
item
= {} item [
sampleKey] = id;

Omkar Mone
Mega Sage

Hi 

you can do something like this :-

 

var temp= {};
		
temp.code = 'code';//Will create key value pair
temp.category = 'cat';


return temp;//can use this wherever you call this script include using temp.code or temp.category.

 

 

Mark Correct if it helps.

 

Warm Regards,

Omkar Mone

www.dxsherpa.com