- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 10:25 PM
I'm getting below in payload:
I only want array inside [] highlighted in bold and then get array "user_name", "action" and "groups_name" and create task for individual. ANy help please
<payload>{"action":"Add to groups","all_groups":"[{\"user_name\":\"a89b564ddb54630002d026f5ca9619d7\",\"uar_current_groups_display\":\"No current Groups\",\"action\":\"Add to groups\",\"groups_name\":\"077ffcd8dbf11058408126f5ca96190d,a8fe1fcbdb39dc1082ac53835b961928\"},{\"user_name\":\"0ee644f897f9115026b4f1dfe153afc0\",\"uar_current_groups_display\":\"No current Groups\",\"action\":\"Add to groups\",\"groups_name\":\"8a63abd1dbd0545047ecfdf96896193f,d832ed1adb605340363190b8db9619f7\"}]","groups_name":"8a63abd1dbd0545047ecfdf96896193f,d832ed1adb605340363190b8db9619f7","subject_person":"f31f775adbfb3454f79a8f104a96190b","uar_current_groups_display":"No current Groups","uar_justification":"test","user_name":"0ee644f897f9115026b4f1dfe153afc0"}</payload>
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 01:14 AM
hi @Hafsa1
print an JSON array directly, which results in the [object Object] message. To properly display the contents of the array, you can convert it to a string using JSON.stringify.
try below code in background script
var payloadString = '{"action":"Add to groups","all_groups":"[{\\"user_name\\":\\"a89b564ddb54630002d026f5ca9619d7\\",\\"uar_current_groups_display\\":\\"Netwrok\\",\\"action\\":\\"Add to groups\\",\\"groups_name\\":\\"077ffcd8dbf11058408126f5ca96190d,a8fe1fcbdb39dc1082ac53835b961928\\"},{\\"user_name\\":\\"0ee644f897f9115026b4f1dfe153afc0\\",\\"uar_current_groups_display\\":\\"No current Groups\\",\\"action\\":\\"Add to groups\\",\\"groups_name\\":\\"8a63abd1dbd0545047ecfdf96896193f,d832ed1adb605340363190b8db9619f7\\"}]","groups_name":"8a63abd1dbd0545047ecfdf96896193f,d832ed1adb605340363190b8db9619f7","subject_person":"f31f775adbfb3454f79a8f104a96190b","uar_current_groups_display":"No current Groups","uar_justification":"test","user_name":"0ee644f897f9115026b4f1dfe153afc0"}';
// Parse the main payload string to a JSON object
var payload = JSON.parse(payloadString);
// Parse the 'all_groups' string to a JSON array
var allGroupsArray = JSON.parse(payload.all_groups);
gs.print('All Groups Array: ' + JSON.stringify(allGroupsArray));
function createTasksForUsers(groupsArray) {
groupsArray.forEach(function(group) {
var userName = group.user_name;
var action = group.action;
var groupsName = group.groups_name;
// Create task (replace with actual task creation logic)
gs.print('Creating task for user:' + userName);
gs.print('Action: ' + group.action);
gs.print("Groups: " + groupsName);
// you would create a task record like this:
/*
var taskGr = new GlideRecord('task');
taskGr.initialize();
taskGr.short_description = 'Task for user: ' + userName;
taskGr.description = 'Action: ' + action + ', Groups: ' + groupsName;
taskGr.insert();
*/
});
}
// Call the function
createTasksForUsers(allGroupsArray);
Please mark helpful & correct answer if it's really worthy for you.
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 10:59 PM
Hi @Hafsa1 ,
You can try something like below
var payloadString = '{"action":"Add to groups","all_groups":"[{\\"user_name\\":\\"a89b564ddb54630002d026f5ca9619d7\\",\\"uar_current_groups_display\\":\\"Netwrok\\",\\"action\\":\\"Add to groups\\",\\"groups_name\\":\\"077ffcd8dbf11058408126f5ca96190d,a8fe1fcbdb39dc1082ac53835b961928\\"},{\\"user_name\\":\\"0ee644f897f9115026b4f1dfe153afc0\\",\\"uar_current_groups_display\\":\\"No current Groups\\",\\"action\\":\\"Add to groups\\",\\"groups_name\\":\\"8a63abd1dbd0545047ecfdf96896193f,d832ed1adb605340363190b8db9619f7\\"}]","groups_name":"8a63abd1dbd0545047ecfdf96896193f,d832ed1adb605340363190b8db9619f7","subject_person":"f31f775adbfb3454f79a8f104a96190b","uar_current_groups_display":"No current Groups","uar_justification":"test","user_name":"0ee644f897f9115026b4f1dfe153afc0"}';
// Parse the main payload string to a JSON object
var payload = JSON.parse(payloadString);
// Parse the 'all_groups' string to a JSON array
var allGroupsArray = JSON.parse(payload.all_groups);
function createTasksForUsers(groupsArray) {
groupsArray.forEach(function(group) {
var userName = group.user_name;
var action = group.action;
var groupsName = group.groups_name;
// Create task (replace with actual task creation logic)
gs.print('Creating task for user:' + userName);
gs.print('Action: ' + group.action);
gs.print("Groups: " + groupsName);
// you would create a task record like this:
/*
var taskGr = new GlideRecord('task');
taskGr.initialize();
taskGr.short_description = 'Task for user: ' + userName;
taskGr.description = 'Action: ' + action + ', Groups: ' + groupsName;
taskGr.insert();
*/
});
}
// Call the function
createTasksForUsers(allGroupsArray);
Please mark helpful & correct answer if it's really worthy for you.
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 12:14 AM
when I'm putting this in background script it is saying:
*** Script: [object Object],[object Object]
+++
var allGroupsArray = JSON.parse(payload.all_groups);
gs.print(allGroupsArray );
+++
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 01:14 AM
hi @Hafsa1
print an JSON array directly, which results in the [object Object] message. To properly display the contents of the array, you can convert it to a string using JSON.stringify.
try below code in background script
var payloadString = '{"action":"Add to groups","all_groups":"[{\\"user_name\\":\\"a89b564ddb54630002d026f5ca9619d7\\",\\"uar_current_groups_display\\":\\"Netwrok\\",\\"action\\":\\"Add to groups\\",\\"groups_name\\":\\"077ffcd8dbf11058408126f5ca96190d,a8fe1fcbdb39dc1082ac53835b961928\\"},{\\"user_name\\":\\"0ee644f897f9115026b4f1dfe153afc0\\",\\"uar_current_groups_display\\":\\"No current Groups\\",\\"action\\":\\"Add to groups\\",\\"groups_name\\":\\"8a63abd1dbd0545047ecfdf96896193f,d832ed1adb605340363190b8db9619f7\\"}]","groups_name":"8a63abd1dbd0545047ecfdf96896193f,d832ed1adb605340363190b8db9619f7","subject_person":"f31f775adbfb3454f79a8f104a96190b","uar_current_groups_display":"No current Groups","uar_justification":"test","user_name":"0ee644f897f9115026b4f1dfe153afc0"}';
// Parse the main payload string to a JSON object
var payload = JSON.parse(payloadString);
// Parse the 'all_groups' string to a JSON array
var allGroupsArray = JSON.parse(payload.all_groups);
gs.print('All Groups Array: ' + JSON.stringify(allGroupsArray));
function createTasksForUsers(groupsArray) {
groupsArray.forEach(function(group) {
var userName = group.user_name;
var action = group.action;
var groupsName = group.groups_name;
// Create task (replace with actual task creation logic)
gs.print('Creating task for user:' + userName);
gs.print('Action: ' + group.action);
gs.print("Groups: " + groupsName);
// you would create a task record like this:
/*
var taskGr = new GlideRecord('task');
taskGr.initialize();
taskGr.short_description = 'Task for user: ' + userName;
taskGr.description = 'Action: ' + action + ', Groups: ' + groupsName;
taskGr.insert();
*/
});
}
// Call the function
createTasksForUsers(allGroupsArray);
Please mark helpful & correct answer if it's really worthy for you.
Thanks,
BK