How to get value from Array of JSON Objects

Surbhi Srivasta
Tera Expert

Hi,

 

I have below output using one of my background script where this is an array of JSON objects.

[{"directory":"446e361e1b35c110336be3fb234bcbdb","roles":"ab348df61badbc10cab09647b04bcb6d"},{"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"ab348df61badbc10cab09647b04bcb6d"},{"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"aed90c9e1b398a50b0c29647b04bcbe1"}]

 

 

in above example, there are 2 directory keys which are same i.e. "3afc390e1b648d10336be3fb234bcb8a". Is it possible to extract value in below format for similar keys like below:

ab348df61badbc10cab09647b04bcb6d, aed90c9e1b398a50b0c29647b04bcbe1

 

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Surbhi Srivasta 

try this and it worked for me

var data = [
  {"directory":"446e361e1b35c110336be3fb234bcbdb","roles":"ab348df61badbc10cab09647b04bcb6d"},
  {"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"ab348df61badbc10cab09647b04bcb6d"},
  {"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"aed90c9e1b398a50b0c29647b04bcbe1"}
];

var result = {};

data.forEach(function(item) {
  if (!result[item.directory]) {
    result[item.directory] = [];
  }
  result[item.directory].push(item.roles);
});

for (var directory in result) {
  gs.info(directory + ': ' + result[directory].join(', '));
}

Output:

AnkurBawiskar_0-1739510903468.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

AshishKM
Kilo Patron
Kilo Patron

Hi @Surbhi Srivasta,

Read the JSON and add the roles value in arrayList after that apply the unique method on arrayList object to get unique records.

 

 

var jsonObj = '[{"directory":"446e361e1b35c110336be3fb234bcbdb","roles":"ab348df61badbc10cab09647b04bcb6d"},{"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"ab348df61badbc10cab09647b04bcb6d"},{"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"aed90c9e1b398a50b0c29647b04bcbe1"}]';
var dataArray = [];
var jsonArray = JSON.parse(jsonObj);
for(var i=0;i<jsonArray.length;i++){
dataArray.push(jsonArray[i].roles);
}
var arrayUtil = new ArrayUtil();  
dataArray= arrayUtil.unique(dataArray);
gs.print("Output: " + dataArray);

 

 

 

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Surbhi Srivasta 

try this and it worked for me

var data = [
  {"directory":"446e361e1b35c110336be3fb234bcbdb","roles":"ab348df61badbc10cab09647b04bcb6d"},
  {"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"ab348df61badbc10cab09647b04bcb6d"},
  {"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"aed90c9e1b398a50b0c29647b04bcbe1"}
];

var result = {};

data.forEach(function(item) {
  if (!result[item.directory]) {
    result[item.directory] = [];
  }
  result[item.directory].push(item.roles);
});

for (var directory in result) {
  gs.info(directory + ': ' + result[directory].join(', '));
}

Output:

AnkurBawiskar_0-1739510903468.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Surbhi Srivasta 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

AshishKM
Kilo Patron
Kilo Patron

Hi @Surbhi Srivasta,

Read the JSON and add the roles value in arrayList after that apply the unique method on arrayList object to get unique records.

 

 

var jsonObj = '[{"directory":"446e361e1b35c110336be3fb234bcbdb","roles":"ab348df61badbc10cab09647b04bcb6d"},{"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"ab348df61badbc10cab09647b04bcb6d"},{"directory":"3afc390e1b648d10336be3fb234bcb8a","roles":"aed90c9e1b398a50b0c29647b04bcbe1"}]';
var dataArray = [];
var jsonArray = JSON.parse(jsonObj);
for(var i=0;i<jsonArray.length;i++){
dataArray.push(jsonArray[i].roles);
}
var arrayUtil = new ArrayUtil();  
dataArray= arrayUtil.unique(dataArray);
gs.print("Output: " + dataArray);

 

 

 

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution