Rapid Network Automation

 View Only

Rapid Network Automation -- Working with JSON Objects!

By Tim Greenside posted 5 hours ago

  

Hello Everyone! 

When working with new low-code/no-code automation users, a common topic of interest is how to work with the JSON objects that get returned from API endpoint queries.  I'll be covering the basics in this blog.

Let's start out with an example scenario.  Maybe I'm a veterinarian, and I need to retrieve information about one of my patients, a cat named "Monty" from an application called CatMD.  It has an API endpoint that returns a JSON object representing a cat record that looks like this:

What can we learn about the mechanics/format of this JSON object?

  • The body of JSON objects are encapsulated with curly braces "{" and "}".
  • JSON objects contain a list of properties in quotes (also known as "keys" ) immediately followed by a colon character (":") which separates the key from its value.  The values can be of various types:
    • "name" (type: string)
    • "age" (type: number)
    • "dispositions" (type: array of comma-delimited string values)
    • "vaccinations" (type: array of JSON objects)
  • All but the last of the property "key/value" pairs within the curly braces must have a comma after them, separating one "key/value" pair from the next one.  
  • String values are encapsulated in quotes, while numbers are not.
  • As seen in this example ("vaccinations" property), it is common for JSON objects to contain other JSON objects within it.  This is sometimes referred to as parent objects containing child objects.
    • The "vaccinations" property contains an array (or list) of JSON objects representing the vaccinations administered to the animal. 
      • "name" (type: string)
      • "administered" (type: string).
    • You will notice that each JSON object in the array is enclosed in its own set of curly braces "{" and "}", and each object is comma-delimited, following the rules of a list of array items.
    • Also notice that each property within the  "vaccination" JSON object curly braces is comma-delimited.

When using a JSON object, it is typically given a variable name to represent the entire object (such as "cat" in our example).

  • To reference the object variable, we prepend a "$" to it.  So "cat" becomes $cat.
  • Now we can refer to the properties within our $cat, using either dot (".") or bracket ([]) notation:
    • $cat.name <or> $cat["name"] -- returns cat name
    • $cat.age <or> $cat["age"] -- returns age of cat
    • $cat.dispositions <or> $cat["dispositions"] -- returns array (list) of dispositions
      • Array items are numbered (enumerated) internally starting at 0 and incrementing by one from there.  To reference the first "disposition" value in the array of $cat.dispositions, you can use $cat.dispositions[0] which will return "crazy", or $cat["dispositions"][1] which will return "aggressive".
    • $cat.vaccinations <or $cat["vaccinations"] -- returns the array of vaccinations JSON objects
      • To access nested "child" JSON object values, you can do the following:
        • $cat.vaccinations returns the entire array of JSON vaccination objects.  To return the first one, remember that array values are enumerated, starting with value 0 (zero).  So we would use $cat.vaccinations[0] to return the first JSON object.  To get the name of that vaccination we would use "$cat.vaccinations[0].name" which would return "distemper".  Using "$cat.vaccinations[0].administered" would return "May 12, 2024".  You can also use bracket notation here such as $cat["vaccinations"][0]["administered"] if you like.

Whew!!!  Ok, now that we've explained the mechanics, lets look at how we might use this object in a low-code/no-code automation.

IBM has a low-code/no-code automation solution known as "Rapid Network Automation".  It allows users to create their own workflows using a drag-and-drop visual interface where you drag and combine "action blocks" representing api endpoints, logic components, existing automation scripts (Python, Ansible), etc. into an orderly workflow, allowing them to automate and accomplish any number of tasks.

All workflows start with a "Start" block, where we can define "variables" which are holders for our data.

As a best practice, when creating your own workflows, it is recommended to use comments to describe what it is that you need to accomplish in your workflow.  This is referred to as defining your workflow using "pseudocode".  IBM's RNA solution provides you with a comment block you can use for this purpose.

In the Start block, we can open it by clicking on the "<>" button on the top of the block and then define our variables:

All start blocks contain an empty variable called "result".  We can use this to return the final result to the user if we like.  

We define an empty "cat" object that will be populated by the CatMD RESTful endpoint query. 

We also define a string variable called "disposition" and populate it with a sample value of "aggressive". 

Being that this is a fictitious application, we will assign our JSON object record for our cat to the $cat variable using an "Assign" block:

Now that we have our $cat record populated, we can iterate through this animal's list of dispositions, looking for an "aggressive" disposition.  We do this by dragging a "ForEach" block onto our workflow designer canvas.  Let's rename it "ForEach_disposition.  We are about to start referencing our $cat JSON object, so we can pull the list of dispositions from it, allowing us to compare them to our "disposition" variable value we set in the start block.

The ForEach block requires an array of values to iterate through.  So we will have it use the $cat.dispositions array, allowing us to iterate through the list of values so we can search for the one we want from the array items.

Within our "ForEach" action block loop, we want to check to see if the current item matches "aggressive" disposition.  To do this we add an "If" action block and define it to compare the current $ForEach_disposition block item ($ForEach_disposition.item) is equal ("==" is for comparision vs. "=" is for assignment) to "aggressive".  (Note:  We could replace "aggressive" with $disposition to use the setting we used in the Start Block, making this more dynamic).

If the $ForEach_disposition.item matches "aggressive" we want to then retrieve the related distemper vaccination record to see when it was last administered to the animal.

To iterate through the vaccinations, below the "If_aggressive" (true) condition we want to add another "ForEach" action block.  Let's rename it "ForEach_vaccination".  We provide it the $cat.vaccinations array to iterate through.  Similar to the original ForEach logic, we will add an "If" action block within the ForEach_vaccination loop, allowing us to look for the vaccination record named "distemper" from the $cat.vaccinations items.  The "ForEach" block array items are referred to using the "item" keyword (which handles them one-by-one without you having to worry about the enumerated array value).  So in our If block, we use the following condition:

  • $ForEach_vaccination.item.name == "distemper"

If found, we will assign the $result variable defined in the Start Block the following variable:

  • $result = $ForEach_vaccination.item.administered

Finally we see our $result in the Logs window:

I hope this has helped to demystify the use of JSON objects in your workflows!


#TechnicalBlog

0 comments
10 views

Permalink