json property extract

Khanna Ji
Tera Guru

Hi Friends - Happy New Year to you all.

I have a custom app and there is a property as shown below. How can I fetch this property values which are in Nested Json format? I tried JSON.parse(property) but its not working in scoped app.

{
"field1": "value1",
"field2": "value1"
},
{
"field1": "value2",
"field2": "value2"
}

 

var msg = gs.getProperty('myproperty');
var parsed = JSON.parse(msg);
for (var i = 0; i < parsed.length; i++){
gs.print(parsed[i].field1);}
1 ACCEPTED SOLUTION

Hi,

the property is already string so don't use JSON.stringify()

I used as I was testing in background script

use this

var prop = gs.getProperty('sn_hr_core.myprop');
var parsed = JSON.parse(prop);
gs.info(parsed.length);
for (var i = 0; i < parsed.length; i++){
	gs.info(parsed[i].field1);
}

Regards
Ankur

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

View solution in original post

13 REPLIES 13

Hi,

it worked fine for me when I ran in BS

Are you running similar script in BS? please share that

Regards
Ankur

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

Propert is in scoped app

find_real_file.png

var prop = gs.getProperty('sn_hr_core.myprop');
var parse = JSON.stringify(prop);
var parsed = JSON.parse(parse);
gs.info(parsed.length);
for (var i = 0; i < parsed.length; i++){
gs.info(parsed[i].field1);
}
*** Script: 95
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined

Hi,

the property is already string so don't use JSON.stringify()

I used as I was testing in background script

use this

var prop = gs.getProperty('sn_hr_core.myprop');
var parsed = JSON.parse(prop);
gs.info(parsed.length);
for (var i = 0; i < parsed.length; i++){
	gs.info(parsed[i].field1);
}

Regards
Ankur

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

@Khanna Ji 

JSON.parse() works in scope app

find_real_file.png

find_real_file.png

Regards
Ankur

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

try it with property, it doesn't work