- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 08:49 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 09:28 PM
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 09:30 PM - edited 02-13-2025 09:40 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 09:28 PM
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2025 07:06 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 09:30 PM - edited 02-13-2025 09:40 PM
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