- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 12:36 AM
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);}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 01:42 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 12:43 AM
Hi,
is the property value returning array of json objects
you should declare the property like this
[{
"field1": "value1",
"field2": "value1"
},
{
"field1": "value2",
"field2": "value2"
}]
Sample Script:
var arr = [{
"field1": "value1",
"field2": "value1"
},
{
"field1": "value2",
"field2": "value2"
}];
var msg = JSON.stringify(arr);
var parsed = JSON.parse(msg);
for (var i = 0; i < parsed.length; i++){
gs.info(parsed[i].field1);}
Output:
Regards
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
01-04-2022 01:13 AM
Its working in BS when I add as you shared but not working like this
var arr = gs.getProperty('myprop');
var msg = JSON.stringify(arr);
var parsed = JSON.parse(msg);
for (var i = 0; i < parsed.length; i++){
gs.info(parsed[i].field1);}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 01:17 AM
Hi,
so at which place it is not working.
if it is working in background script then it should work fine at other place
are you in same scope or some scope issue is there?
what error you get or what logs you get when you use gs.info() in the actual script?
Regards
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
01-04-2022 01:21 AM
Getting below as an output when I run it in BS. Also when I printed length it gives me 95 which is not correct. I have just two elements in the property.
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined
*** Script: undefined