How do I extract the data from a JSON Object in Flow Designer?

gjz
Mega Sage

I have a business requirement to find all groups a specific user is a member of in Active Directory and write the groups to the work notes on a record.  There isn't an OOB action to do this for a specific user so I created my own.  The action is working, but I can't figure out how to extract the data so I can write it to the record in Flow Designer.

 

The action returns the groups in a JSON object, but when I try to use the "For Each" flow logic , it doesn't allow me to select the output from the previous step.

 

Here is my action's output:

 

gjz_2-1748467838240.png

 

This is the flow I'm using to test my action and output - as you can see, I can't select the Groups from the previous step.

gjz_4-1748468191456.png

This is what the "Groups" output looks like from the 2nd step:

gjz_5-1748468494694.png

 

 

 

 

 

 

 

 
 
1 ACCEPTED SOLUTION

@gjz 

I attached gif now here.

you are storing json string in the script step output, but it should be object

if the input to script step is json object then don't use JSON.stringify()

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

18 REPLIES 18

Sandeep Rajput
Tera Patron
Tera Patron

@gjz Your custom action return a JSON string whereas For each item action expects an array. I recommend updating your custom action output to return an array of objects instead of a JSON value. Once done and changes are published, you will be able to select Group object in For each item action. 

Yes, I assume that was what I need to do, but I don't know how to do it.  I can't change what is returned from Active Directory, but the action uses the following code, how do I change it and the output variable?  There are multiple array data type options.

 

    function formatSingleObjResponse(objResponse) {
        for (var key in objResponse) {
            var itemValue = objResponse[key];
            if (itemValue instanceof Array) {
                var stringValue = itemValue.join(" ");
                objResponse[key] = stringValue;
            }
            if (key == 'objectSid' && itemValue['Value']) objResponse[key] = itemValue['Value'];
            if (key == 'ObjectGuid' && objResponse['ObjectGUID']) delete objResponse['ObjectGuid'];
        }
    }

    if (inputs.errorMessage)
        handleError(inputs.errorMessage);
    else {
        var responseBody = JSON.parse(inputs.responseBody);
        if (responseBody.status == "Success") {
            outputs.response = JSON.stringify(responseBody.body);
            outputs.records_found = false;
            outputs.number_of_records = 0;
            if (responseBody.body instanceof Array) {
                outputs.records_found = true;
                outputs.number_of_records = (responseBody.body).length;
            } else {
                if (Object.keys(responseBody.body).length !== 0) {
                    formatSingleObjResponse(responseBody.body);
                    var responseArray = [];
                    responseArray.push(responseBody.body);
                    outputs.response = JSON.stringify(responseArray);
                    outputs.records_found = true;
                    outputs.number_of_records = 1;
                }
            }
        } else if (responseBody.status == "Error")
            if (responseBody.body)
                handleError(responseBody.body);
            else
                handleError("Unknown error. Please check error log for more information");
        else
            handleError("Unknown error. Please check error log for more information");
    }

@gjz You need to create another custom action, which will take output of the Active Directory action as an input  (JSON value) and convert it into an array and output that array. This array can then be enumerated via a for each loop. Hope this helps.

I guess I haven't explained what I need very well.  I don't know how to do what you are recommending and am asking for help.  I shouldn't have to create another action, if you look at the screen shot of the custom action, it already has a Post Processing & Handling step that takes the output and converts it to a JSON object.  My assumption is the code in that step needs to be modified to turn it into an array, but again, I don't know how to script that.