- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 04:26 AM
Hi,
We need to send variable's information for different catalog items to a 3rd party tool in JSON format. We need to extract all the variable information and need to send it in below JSON format:
{"Variables":[{"variable1 Label":"Variable 1 Value","variable2 Label":"Variable 2 Value","variable3 Label":"Variable 3 Value"}]}
Something like as shown below:
{
"Variables": [{
"variable1 Label": "Variable 1 Value",
"variable2 Label": "Variable 2 Value",
"variable3 Label": "Variable 3 Value"
}]
}
I am able to get all variable information using the script mentioned below:
var gr = new GlideRecord('sc_task');
gr.addEncodedQuery('sys_id=d6fb6749db24c89435dc403c3a961985');
gr.query();
while(gr.next()){
for(var i in gr.variables){
if (gr.variables.hasOwnProperty(i))
{
var variable = gr.variables[i];
gs.info(i+ ':' + variable);
}
}
}
Can someone please help me out in how to build this JSON structure.
Kindly assist!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 05:08 AM
Hi,
use below script to store that in the required json format
not sure I am not able to get the variables names using your script; based on sc_task so I queried sc_request_item table after your code
use below script and it will print the exact json format you want
var gr = new GlideRecord('sc_task');
gr.addEncodedQuery('sys_id=d6fb6749db24c89435dc403c3a961985');
gr.query();
if(gr.next()){
var jsonObj = {};
var ritmSysId = gr.request_item;
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritmSysId);
set.load();
var vs = set.getFlatQuestions();
var arr = [];
var obj = {};
for(var i=0;i<vs.size();i++){
var label = vs.get(i).getLabel();
var value = vs.get(i).getDisplayValue();
obj[label] = value.toString();
}
arr.push(obj);
jsonObj.Variables = arr;
gs.info(JSON.stringify(jsonObj));
}
sample output when I tested in my case; the exact format you wanted
*** Script: {"Variables":[{"Cost Center":"Customer Support","Project":"ITIL Project","Test User":"Abel Tuter1","Specify the time when you want the token link to be initially active":"2019-10-23 10:48:21","Hours":"1","Minutes":"0","Select the policy to be attached to the token":"Super Admin Policy","Specify reason for requesting a token":"New User Login"}]}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 04:47 AM
You can either get object like {"var_label": "var_value"} see example below:
var gr = new GlideRecord('sc_task');
gr.addEncodedQuery('sys_id=d6fb6749db24c89435dc403c3a961985');
gr.query();
var answer = {};
while (gr.next()){
for (var i in gr.variables){
if (gr.variables.hasOwnProperty(i)) {
var variable = gr.variables[i];
answer[variable.getLabel()] = variable.getDeisplayValue();
}
}
}
Or you can get array of objects [{"label": "var_label", "value": "var_value"}] see example below:
var gr = new GlideRecord('sc_task');
gr.addEncodedQuery('sys_id=d6fb6749db24c89435dc403c3a961985');
gr.query();
var answer = [];
while (gr.next()){
for (var i in gr.variables){
if (gr.variables.hasOwnProperty(i)) {
var variable = gr.variables[i];
var object = {
"label": variable.getLabel(),
"value": variable.getDeisplayValue()
};
answer.push(object);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 06:42 PM
Hi,
Thanks for your response, but it didn't worked for me. It gives me empty labels of the variables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 05:08 AM
Hi,
use below script to store that in the required json format
not sure I am not able to get the variables names using your script; based on sc_task so I queried sc_request_item table after your code
use below script and it will print the exact json format you want
var gr = new GlideRecord('sc_task');
gr.addEncodedQuery('sys_id=d6fb6749db24c89435dc403c3a961985');
gr.query();
if(gr.next()){
var jsonObj = {};
var ritmSysId = gr.request_item;
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritmSysId);
set.load();
var vs = set.getFlatQuestions();
var arr = [];
var obj = {};
for(var i=0;i<vs.size();i++){
var label = vs.get(i).getLabel();
var value = vs.get(i).getDisplayValue();
obj[label] = value.toString();
}
arr.push(obj);
jsonObj.Variables = arr;
gs.info(JSON.stringify(jsonObj));
}
sample output when I tested in my case; the exact format you wanted
*** Script: {"Variables":[{"Cost Center":"Customer Support","Project":"ITIL Project","Test User":"Abel Tuter1","Specify the time when you want the token link to be initially active":"2019-10-23 10:48:21","Hours":"1","Minutes":"0","Select the policy to be attached to the token":"Super Admin Policy","Specify reason for requesting a token":"New User Login"}]}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 06:44 PM
Hi Ankur,
Thank you so much for your response. The code works perfectly for me. Just one question before we close this thread, is there a way Variables which are of type = Label can be excluded from getting printed in the final output?