How to Iterate Over a JSON Object (or How to Use the Object – For Each Card)

Okta Workflows how-to guides are questions and answers from weekly community office hours, MacAdmins Workflows Slack channel, and other places. Read all other how-to guides

Thanks to Bishal Shrestha, an Okta Administrator at Pinterest, who asked this question in MacAdmins Slack.

On to the question. 

How do you iterate over a JSON object to retrieve a path value?

Starting with a JSON object

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 to pass 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 how to use Map – For Each card.

What you learned

In this blog post, you learned how to use the Object – For Each card to iterate over a JSON object.


More resources to help you learn:

🍫 Get help from Workflows specialists during weekly community office hours.

📺 Learn from short Workflows videos.

🙋🏻‍♀️ Join the #okta-workflows channel on MacAdmins Slack to learn and get help from the community

Published by

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.