- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 10:48 PM
I have a payload that contains values as array. What is the best way to parse this?
Values can have multiple values in them.
{
"allFailedServersSysId" : [ "b7fd94681bfa9010c63e55392a4bcbb8" ],
"appCount" : "One App",
"appRTM" : [ "91579bd61021a800df0bd7621fe7e6aa" ],
"appServSysId" : [ "1b154ff71b8a58902ac554e56e4bcb6b","53154ff71b8a58902ac554e56e4bcb59" ],
"appSupportGroup" : [ "fa7f9bd310ada800df0bd7621fe7e6fa" ],
"server" : "vdwvmwsdb69",
"serverSysId" : "b7fd94681bfa9010c63e55392a4bcbb8"
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 11:01 PM
Hi Kumar,
If it's a JSON, there's no need to parse.
For example:
var json = {"allFailedServersSysId":["b7fd94681bfa9010c63e55392a4bcbb8"],"appCount":"One App","appRTM":["91579bd61021a800df0bd7621fe7e6aa"],"appServSysId":["1b154ff71b8a58902ac554e56e4bcb6b","53154ff71b8a58902ac554e56e4bcb59"],"appSupportGroup":["fa7f9bd310ada800df0bd7621fe7e6fa"],"server":"vdwvmwsdb69","serverSysId":"b7fd94681bfa9010c63e55392a4bcbb8"};
gs.print(json.allFailedServersSysId);
Will result in
*** Script: b7fd94681bfa9010c63e55392a4bcbb8
However, if it's actually a string. For example, (note the "'" surrounding the json)
var json = '{"allFailedServersSysId":["b7fd94681bfa9010c63e55392a4bcbb8"],"appCount":"One App","appRTM":["91579bd61021a800df0bd7621fe7e6aa"],"appServSysId":["1b154ff71b8a58902ac554e56e4bcb6b","53154ff71b8a58902ac554e56e4bcb59"],"appSupportGroup":["fa7f9bd310ada800df0bd7621fe7e6fa"],"server":"vdwvmwsdb69","serverSysId":"b7fd94681bfa9010c63e55392a4bcbb8"}';
var jsonObj = JSON.parse(json);
gs.print(jsonObj .allFailedServersSysId);
there is a need to parse. Use JSON.parse.
Above script will result in below
*** Script: b7fd94681bfa9010c63e55392a4bcbb8

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 11:01 PM
Hi Kumar,
If it's a JSON, there's no need to parse.
For example:
var json = {"allFailedServersSysId":["b7fd94681bfa9010c63e55392a4bcbb8"],"appCount":"One App","appRTM":["91579bd61021a800df0bd7621fe7e6aa"],"appServSysId":["1b154ff71b8a58902ac554e56e4bcb6b","53154ff71b8a58902ac554e56e4bcb59"],"appSupportGroup":["fa7f9bd310ada800df0bd7621fe7e6fa"],"server":"vdwvmwsdb69","serverSysId":"b7fd94681bfa9010c63e55392a4bcbb8"};
gs.print(json.allFailedServersSysId);
Will result in
*** Script: b7fd94681bfa9010c63e55392a4bcbb8
However, if it's actually a string. For example, (note the "'" surrounding the json)
var json = '{"allFailedServersSysId":["b7fd94681bfa9010c63e55392a4bcbb8"],"appCount":"One App","appRTM":["91579bd61021a800df0bd7621fe7e6aa"],"appServSysId":["1b154ff71b8a58902ac554e56e4bcb6b","53154ff71b8a58902ac554e56e4bcb59"],"appSupportGroup":["fa7f9bd310ada800df0bd7621fe7e6fa"],"server":"vdwvmwsdb69","serverSysId":"b7fd94681bfa9010c63e55392a4bcbb8"}';
var jsonObj = JSON.parse(json);
gs.print(jsonObj .allFailedServersSysId);
there is a need to parse. Use JSON.parse.
Above script will result in below
*** Script: b7fd94681bfa9010c63e55392a4bcbb8
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 11:18 PM
When I try to run this in my background script , I'm getting this error. Is it because of my payload format. fyi , its build in script.
Evaluator: com.glide.script.RhinoEcmaError: Unexpected token: o script : Line(155) column(0)
This line is throwing error.
var jsonObj = JSON.parse(json);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 11:33 PM
Is the error happening the script I've pasted.
If not, is the payload coming in as a string or as an object. If it's an object, there's no need to parse.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 11:06 PM
If the question is on getting an array element, it's just an Javascript array.
var json = '{"allFailedServersSysId":["b7fd94681bfa9010c63e55392a4bcbb8"],"appCount":"One App","appRTM":["91579bd61021a800df0bd7621fe7e6aa"],"appServSysId":["1b154ff71b8a58902ac554e56e4bcb6b","53154ff71b8a58902ac554e56e4bcb59"],"appSupportGroup":["fa7f9bd310ada800df0bd7621fe7e6fa"],"server":"vdwvmwsdb69","serverSysId":"b7fd94681bfa9010c63e55392a4bcbb8"}';
var jsonObj = JSON.parse(json);
var appServSysId = jsonObj.appServSysId;
gs.print(appServSysId); // print out array
gs.print("====================");
for (var i=0; i<appServSysId.length; i++) { // print out each individual element in an array
gs.print(i + ' ' + appServSysId[i]);
}
Execution result. Note that printing out an array will print out element is comma delimited
*** Script: 1b154ff71b8a58902ac554e56e4bcb6b,53154ff71b8a58902ac554e56e4bcb59
*** Script: ====================
*** Script: 0 1b154ff71b8a58902ac554e56e4bcb6b
*** Script: 1 53154ff71b8a58902ac554e56e4bcb59