Okta Workflows How-To: Iterate Over a JSON Object (or How to Use the Object – For Each Card)
Okta Workflows is a no-code platform for automating identity processes.
This blog post is based on a question asked during office hours or the #okta-workflows channel in MacAdmins Slack: How do you iterate over a JSON object to retrieve a path value?
Thanks to Bishal Shrestha, an Okta Administrator at Pinterest, who asked this question in MacAdmins Slack.
Getting started
You have a JSON object, most likely from an API call, that looks like this:
{ "file_type": "Locations", "file_content": { "Location 1": { "ipv4_allowlist": [ "128.10.10.01", "128.10.10.02" ], "description": "Location 1 gateway" }, "Loation 2": { "ipv4_allowlist": [ "128.10.10.03", "128.10.10.04" ], "description": "Location 2 gateway" }, "Location 3": { "ipv4_allowlist": [ "128.10.10.05", "128.10.10.06" ], "description": "Location 3 gateway" }, "Location 4": { "ipv4_allowlist": [ "128.10.10.07", "128.10.10.08", "128.10.10.09" ], "description": "Location 4 gateway" } } }
You want to go over this JSON and get only the IP addresses. To get the IP addresses, use the Object – For Each card. Let’s look at an example flow.
Using For Each to iterate over a JSON object
This is the main flow:
Using Object – For Each card
On the screenshot above:
- Most likely you would get the JSON from an API call. In this example, the JSON is created using JSON – Parse card. Note that file_content (object) path is defined under the output path. This allows passing the file_content path to the next card
- The file_content path is passed to the Object – For Each card. The card calls the Map_ForEach_helper flow. The card passes a key and a value to the helper flow. Looking at the JSON file, the key is Location 1 and its value.
Helper flow to iterate over a JSON object
This is the helper flow:
Using Object – For Each helper flow
In this helper flow:
- The Helper Flow card defines two inputs: Key (string) and Value (object)
- Using the Get Multiple card, ipv4_allowlist retrieves the IP addresses from the object
Running the flows to get the IP addresses
This screenshot shows how the helper flow works:
Helper flow history
Updating the flow to retrieve each IP address
The ipv4_allowlist field now holds the IP address that we want to retrieve. But, it’s a list of IP addresses. Let’s update the flow so you can get each individual IP address.
First, you are going to add another helper flow:
Helper flow to get an IP address
In this helper flow:
- An IP address is passed to this flow and then assigned to IP Address field (using the Assign card) for displaying purpose
In the first helper flow, where you get a list of IP addresses, you are going to add a List – For Each card:
Iterating over each IP address with List – For Each card
In this flow:
- When you have a list of IP addresses, this list is then passed to the For Each card where you get each individual IP address
Flow history retrieving one IP address:
Retrieving an IP address
Video
Watch a short video about these flows.
What you learned
In this blog post, you learned how to use the Object – For Each card to iterate over a JSON object.
Get your Workflows questions answered
Do you have a question about Okta Workflows? Not sure how to build a flow? Join the weekly community office hours to get help.