The CreatorCon Call for Content is officially open! Get started here.

Parse array values in JSON key value pair

Kumar38
Kilo Sage

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"
}
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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

View solution in original post

4 REPLIES 4

Hitoshi Ozawa
Giga Sage
Giga Sage

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

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);

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.

 

Hitoshi Ozawa
Giga Sage
Giga Sage

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