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

it worked, thanks you.

Abhijit4
Mega Sage

Hi,

Please try below,

var arr=gs.getProperty("myprop");

var parsed = JSON.parse(arr);
for (var i = 0; i < parsed.length; i++){
gs.info(parsed[i].field1);}

 

put value in property like below

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

Its working for me in my PDI.

Let me know if you have any further queries.

Please mark this as Correct or helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

JSON object doesn't work in scoped app.

try with below once

 

var arr=gs.getProperty("myprop");

var parsed = global.JSON.parse(arr);
for (var i = 0; i < parsed.length; i++){
gs.info(parsed[i].field1);}

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP