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

Ankur - between you and the explanation from @Zach N, it works!  I am so relieved!  Thank you so much for being patient and thanks Zach for the explanation of the object.

Awesome! Glad it is working! Nothing like finding a solution after banging your head against a wall for a couple of days.

John Gilmore
Giga Guru

@gjz The issue you are running into is caused by the expected input for the For Each flow logic. The input type it expects is "Records". In your case what you are processing cannot be a list of records so will not work as an input for your the "For Each" flow logic.

As the information you are pulling is coming from outside of ServiceNow it likely can't be a list of records. In order to perform what you are attempting you will need to perform the for each inside your scripted action or across multiple actions.

My recommendation would be to add a new scripted action specifically to update the notes on the record. Change the output for your JSON to be a string and when setting it at the end of your post processing make sure to use JSON.stringify() or have the string already represent the exact content you want for the updated note. Then you can use the JSON.parse('inputs.json_variable') method at the start of your next action to make it more usable for the record update script if it isn't already the exact string you want to be the work note text.

var groupsAD = JSON.parse('inputs.groups');
var gr = new glideRecord('Table'); //Table is the table containing the record of which you want to update the work notes.

//Insert code here to format the groups information as a string as you want it to appear in the notes if you didn't do this during the post processing action. The following assumes you just want exactly what is in the json output of the post processing action concatenated as a string where separator represents a comma or whatever you want to separate the groups.
var groupsWorkNote = groupsAD.join('separator');

//Get the record you are updating the notes on and push the update.
gr.get('sys_ID of record'); //This gets the record assuming you know the sys_ID, this is easy if for example the trigger record is always the record being updated.
gr['work_notes'] = groupsWorkNote; //Work Notes is not accessed the same way as normal columns because its a journal column so the gr['work_notes'] method is the easiest way.
gr.update();

 

It could be simplified if your input is already a string that you want to be the content of the work note:

var groupsWorkNote = inputs.groups;
var gr =  new glideRecord('Table');
gr.get ('sys_ID');
gr['work_notes'] = groupsWorkNote;
gr.update();

Zach N
Tera Guru

As others have mentioned, to cycle through the groups in you Flow you'll need to convert your JSON string into an array of JavaScript objects. Based on your posts, here is the JSON I used in my testing:

 

{
    "body": [
    {
        "distinguishedName": "CN=NO Access,OU=Groups,DC=blarg"
    },
    {
        "distinguishedName": "CN=Domain Users,OU=Groups,DC=secondblarg"
    },
    {
        "distinguishedName": "CN=SN_Workday_ITS_Maintenance,OU=Groups,DC=thirdblarg"
    }
],
"status": "Success"
}

 

In the script step of your action, you can convert this to a JavaScript object using JSON.parse():

 

var groupObj = JSON.parse(inputs.<json_input_variable>);

 

Replace <json_input_variable> with the variable containing your JSON string (I'm assuming this is an output from your PowerShell step).

 

This will create an object with the following structure:

 

  • Object
    • Array.Object
      • Object
        • Property: distinguishedName
  • Property: Status

Since we want to loop through this in a Flow, we only care about Array.Object. In the script step, we can add this line:

 

outputs.groups = groupObj.body;

 

 Then in our output variables for the step, create the following:

 

  • Groups - groups - Array.Object
    • Group - group - Object
      • Distinguished Name - distinguished_name - String

Repeat this for our action outputs, and add the script step variable data pill to our action outputs.

 

Finally, in our flow we can loop through the Groups object, and look at the Distinguished Name of each. In general when working with JSON, JavaScript Objects, and Action Outputs, we want to try to match the structure of the object in our output variables.

 

Based on your other replies I'd forgo the Post Processing & Handling step for now until you get this piece working, as that might be muddying the waters.

 

See attached for some screenshots.

 

Hope this helps! Let me know if you have any questions.