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

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.

rakesh18081989
Tera Contributor

for(i=0;i<5;i++)

{

 var myVar = {};

myVar[i] = i;

}

 

gs.log(myVar[1]+" "+myVar[2]+" "+myVar[3]);

output: 1 2 3

 

Mark Correct if it helps.

 

Warm Regards,

Rakesh Agarwal

Mahendra RC
Mega Sage

Hi Nidhi,

I am not sure where you are using, but I can give you the idea on how to store Json object dynamically. Please refer the below code which will give you idea how to store Json object dynamically..

(function () {
var incidentJson = {};
gs.log("incident1");
var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true');
gr.setLimit(3);
gr.query();
while (gr.next()) {
incidentJson[gr.getValue("number")] = {};
// this will give you {"INC09991": {}}
incidentJson[gr.getValue("number")]['caller_id'] = gr.getValue("caller_id");
// this will give you {"INC09991": {"caller_id": "5137153cc611227c000bbd1bd8cd2005"}}
incidentJson[gr.getValue("number")]['category'] = gr.getValue("category");
// this will give you {"INC09991": {"caller_id": "5137153cc611227c000bbd1bd8cd2005","category":"network"}}
incidentJson[gr.getValue("number")]['state'] = gr.getValue("state");
// this will give you {"INC09991": {"caller_id": "5137153cc611227c000bbd1bd8cd2005","category":"network","state":"3"}}
incidentJson[gr.getValue("number")]['priority'] = gr.getValue("priority");
// this will give you {"INC09991": {"caller_id": "5137153cc611227c000bbd1bd8cd2005","category":"network","state":"3","priority":"1"}}
incidentJson[gr.getValue("number")]['urgency'] = gr.getValue("urgency");
// this will give you {"INC09991": {"caller_id": "5137153cc611227c000bbd1bd8cd2005","category":"network","state":"3","priority":"1","urgency":"1"}}
incidentJson[gr.getValue("number")]['impact'] = gr.getValue("impact");
// this will give you {"INC09991": {"caller_id": "5137153cc611227c000bbd1bd8cd2005","category":"network","state":"3","priority":"1","urgency":"1","impact":"1"}}
}
gs.log("incident JSON : " + JSON.stringify(incidentJson));

var variableName = "incidentNumber"; // This is the variable name stored
var variableValue = "INC00001234"; // This is the variable value stored
var incJson = {};
incJson[variableName] = variableValue;
gs.log("incJson : " + JSON.stringify(incJson));
})();

 

OUTPUT: 

*** Script: incident1
*** Script: incident JSON :

{"INC0000002":{"caller_id":"5137153cc611227c000bbd1bd8cd2005","category":"network","state":"3","priority":"1","urgency":"1","impact":"1"},"INC0000003":{"caller_id":"681ccaf9c0a8016400b98a06818d57c7","category":"network","state":"2","priority":"1","urgency":"1","impact":"1"},"INC0000007":{"caller_id":"681ccaf9c0a8016400b98a06818d57c7","category":"database","state":"3","priority":"1","urgency":"1","impact":"1"}}
*** Script: incJson : {"incidentNumber":"INC00001234"}

Please mark this correct or helpful, if it resolves your problem.

Thanks,

Mahendra

 

abhisheknirwan1
Kilo Contributor

Hi Nidhi,

Did you find a solution to it. Can you please share the code.

 

Thanks in advance