We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to get Json data from field through business rule.

Sai25
Tera Guru

Hi All,

How to pull below Json data from a field. I am storing the Json data on form in a  particular field that is string field.

The below data is from  a catalog item details.

{
'sysparm_id': '0d08837237153000158bbfc8bcbe5d02',
'sysparm_quantity': '1',
'variables':{
'example_var1': 'at_and_t_mobility',
'example_var2': '500MB',
'example_var3': 'eighteen_months',
'example_var4': 'slate',
'example_var5': 'sixtyfour'
}};

I want to get the variables and sysparm_id. How to get from a variables.

Thank You,

Sai.

1 ACCEPTED SOLUTION

hvrdhn88
Giga Patron

try now. 

 

var json = {
'sysparm_id': '0d08837237153000158bbfc8bcbe5d02',
'sysparm_quantity': '1',
'variables':{
'example_var1': 'at_and_t_mobility',
'example_var2': '500MB',
'example_var3': 'eighteen_months',
'example_var4': 'slate',
'example_var5': 'sixtyfour'
}};


var ab = JSON.stringify(json);
var op = JSON.parse(ab);

gs.print(op.sysparm_id);

gs.print(op.variables.example_var1); // this way you will get variables object details. 

View solution in original post

5 REPLIES 5

hvrdhn88
Giga Patron

try now. 

 

var json = {
'sysparm_id': '0d08837237153000158bbfc8bcbe5d02',
'sysparm_quantity': '1',
'variables':{
'example_var1': 'at_and_t_mobility',
'example_var2': '500MB',
'example_var3': 'eighteen_months',
'example_var4': 'slate',
'example_var5': 'sixtyfour'
}};


var ab = JSON.stringify(json);
var op = JSON.parse(ab);

gs.print(op.sysparm_id);

gs.print(op.variables.example_var1); // this way you will get variables object details. 

Hi Harsha,

I have tested this through background script its working.

When I run the same code through business rule its not working.

var json = { // This Json name is not there in my data.
'sysparm_id': '0d08837237153000158bbfc8bcbe5d02',
'sysparm_quantity': '1',
'variables':{
'example_var1': 'at_and_t_mobility',
'example_var2': '500MB',
'example_var3': 'eighteen_months',
'example_var4': 'slate',
'example_var5': 'sixtyfour'
}};

The json data like below.

{
'sysparm_id': '0d08837237153000158bbfc8bcbe5d02', (this is the sys_id)
'sysparm_quantity': '1',
'variables':{
'example_var1': 'at_and_t_mobility',
'example_var2': '500MB',
'example_var3': 'eighteen_months',
'example_var4': 'slate',
'example_var5': 'sixtyfour'
}};


var ab = JSON.stringify(current.description);
var op = JSON.parse(ab);

gs.addInfoMessage(op.sysparm_id);
gs.info(op.sysparm_id);
gs.info(op.variables.example_var1);

 

Thank You,

Sai.

share your business rule code, you have to directly use the JSON.parse() 

 

 

eg:

 

var ab = current.getValue('JSON Field name');

var op = JSON.parse(ab);

gs.log('JSON value is '+ op); // it should come object object

gs.addInfoMessage(op.sysparm_id);
gs.info(op.sysparm_id);
gs.info(op.variables.example_var1);