Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get all variables in JSON

snow_04
Giga Contributor

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!!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

14 REPLIES 14

Hi Amritha,

for mrvs you will have to check in this table

sc_multi_row_question_answer

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi  @Ankur Bawiskar 

I am able to get all variables from requested item record excepts for Multi row variables. How can I include Multi row variables as well in same JSON? Please let me know.

 

Thanks,

Sindhu.

MRVS holds json structure.

Data for that is present in sc_multi_row_question_answer table.

you can get it from there and use.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you.. I tried querying 'sc_multi_row_question_answer' table, it works but I am seeing two issues here.

1) It only displays 1 row variables instead of multi row 

2) It displays sys_id for reference fields instead of Display value

Is there any way we can achieve this.. Please refer the code below

var mrv = new GlideRecord('sc_multi_row_question_answer');
mrv.addQuery('parent_id',ritmsys_id);
mrv.query();
while (mrv.next()) {

var variableValue = mrv.value();

var variableLabel = mrv.item_option_new.question_text;

 

obj[variableLabel] = variableValue.toString();

}

arr.push(obj);

jsonObj.Variables = arr;

 

gs.print(JSON.stringify(jsonObj));

}

Ajay_Chavan
Kilo Sage

Hi There , You dont have to write any lengthy code for this , its pretty simple check out below:

var jsonstring={"Variables":[{"lable1":"ajay1","lable2":"ajay2","lable3":"ajay3"}]};

gs.log(jsonstring.Variables[0].lable1);
gs.log(jsonstring.Variables[0].lable2);
gs.log(jsonstring.Variables[0].lable3);

 

Output as below:

 

find_real_file.png

 

If this resolves your query, please mark my comments as correct find_real_file.png and helpfulfind_real_file.png  .

Regards,

Ajay Chavan

My Community Articles

LinkedIn

 

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****