Parse JSON string from Microsoft AD Spoke Lookup User

Dazler
Mega Sage

Hi,

 

When using the Microsoft AD Spoke, Lookup User action in Flow Designer, I set the property list to return the user's groups from AD.  The attribute in AD is MemberOf.

 

The lookup is working and I retrieve the user's memberof groups in a JSON String.  This string can't be used in a for each loop, so I created a custom action to parse the JSON string.  However, I am having a hard time.

 

Some users are only in 1 group, so the JSON looks like this.

{
   "MemberOf":"CN=Bright Executives,OU=SNOW,OU=Groups,OU=Connected,DC=Dev,DC=Green,DC=com"
}

 

Then there are some that have multiple groups and this is how the return looks.

{
"MemberOf":
	[
	"CN=Group_22c6,OU=MGroups,OU=Groups,OU=Resources,OU=Context,DC=Green,DC=com",
	"CN=Bright Executives,OU=SNOW,OU=Groups,OU=Connected,DC=Dev,DC=Green,DC=com"
]
}

 

I tried to use the Parser step action in flow, but it is based on a structure and either option doesn't fit the other.  I even tried just coding it directly in a script but my return looks like this.

 

CN=Group_22c6,OU=MGroups,OU=Groups,OU=Resources,OU=Context,DC=Green,DC=com,CN=Bright Executives,OU=SNOW,OU=Groups,OU=Connected,DC=Dev,DC=Green,DC=com

If I tried to turn this into an array, it would loop through all the items, separated by comma.  That could get long especially if they have more than 10 groups.

Because when you return groups from Active Directory, it returns as the distinguishedname and not just the display name.  The real information that I need from these are the CN= text.

 

I need to be able to push the items into an array, no matter if it is just 1 group or multiple so that I can use the foreach loop.

 

Does anyone have any ideas, how I can go about this?

1 ACCEPTED SOLUTION

jaheerhattiwale
Mega Sage
Mega Sage

@Dazler Tried and tested solution. Parse it using the code

 

var result = [];
var object = {
   "MemberOf":"CN=Bright Executives,OU=SNOW,OU=Groups,OU=Connected,DC=Dev,DC=Green,DC=com"
}

/*var object = {
"MemberOf":
    [
    "CN=Group_22c6,OU=MGroups,OU=Groups,OU=Resources,OU=Context,DC=Green,DC=com",
    "CN=Bright Executives,OU=SNOW,OU=Groups,OU=Connected,DC=Dev,DC=Green,DC=com"
]
}*/

if(typeof(object.MemberOf) == "string"){
    var element = object.MemberOf;
    element = element.split(",");

    for(var j=0; j<element.length; j++){
        if(element[j].startsWith("CN=")){
            result.push(element[j]);
        }
    }
}else if(typeof(object.MemberOf) == "object"){
    var innerArray = object.MemberOf;

    for(var i=0; i<innerArray.length; i++){
        var element = innerArray[i];
        element = element.split(",");

        for(var j=0; j<element.length; j++){
            if(element[j].startsWith("CN=")){
                result.push(element[j]);
            }
        }
    }
}

gs.info(result.toString())
 
Result 1 (when users only in one group):
jaheerhattiwale_0-1670057130394.png

 

Result 2 (when user is part of multiple group):

jaheerhattiwale_1-1670057190930.png

 

 

Please mark as correct answer if this solves your issue.

 
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

10 REPLIES 10

Claude DAmico
Kilo Sage

Are you able to push to an array by calling from the JSON returned? I tested one of your JSON outputs to see if this would work and it did in a background script. Just need to make sure the format stays this way otherwise you would need to adjust the substring by determining the index of the starting and ending points of the strings in the array.

 

var obj = {"MemberOf":["CN=Group_22c6,OU=MGroups,OU=Groups,OU=Resources,OU=Context,DC=Green,DC=com","CN=Bright Executives,OU=SNOW,OU=Groups,OU=Connected,DC=Dev,DC=Green,DC=com"],"test":"test"};
var array = [];
for(var i = 0; i < obj.MemberOf.length; i++){
   array.push(obj.MemberOf[i].substring(3,obj.MemberOf[i].indexOf(",")));
}

 

Claude E. D'Amico, III - CSA

Hi @Claude DAmico 

 

When my output is multiple groups returned, then yes the format pretty much stays the same.  I tested your code in background script and it worked.  However, I still have to solve for if a single group is returned.  That format is different.

jaheerhattiwale
Mega Sage
Mega Sage

@Dazler Tried and tested solution. Parse it using the code

 

var result = [];
var object = {
   "MemberOf":"CN=Bright Executives,OU=SNOW,OU=Groups,OU=Connected,DC=Dev,DC=Green,DC=com"
}

/*var object = {
"MemberOf":
    [
    "CN=Group_22c6,OU=MGroups,OU=Groups,OU=Resources,OU=Context,DC=Green,DC=com",
    "CN=Bright Executives,OU=SNOW,OU=Groups,OU=Connected,DC=Dev,DC=Green,DC=com"
]
}*/

if(typeof(object.MemberOf) == "string"){
    var element = object.MemberOf;
    element = element.split(",");

    for(var j=0; j<element.length; j++){
        if(element[j].startsWith("CN=")){
            result.push(element[j]);
        }
    }
}else if(typeof(object.MemberOf) == "object"){
    var innerArray = object.MemberOf;

    for(var i=0; i<innerArray.length; i++){
        var element = innerArray[i];
        element = element.split(",");

        for(var j=0; j<element.length; j++){
            if(element[j].startsWith("CN=")){
                result.push(element[j]);
            }
        }
    }
}

gs.info(result.toString())
 
Result 1 (when users only in one group):
jaheerhattiwale_0-1670057130394.png

 

Result 2 (when user is part of multiple group):

jaheerhattiwale_1-1670057190930.png

 

 

Please mark as correct answer if this solves your issue.

 
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Thank you, @jaheerhattiwale!

 

This worked.  I appreciate the help.  Thank you again.