How to iterate over JSON objects in ZebraTester using Inline Scripts

During ZebraTester scripting you may encounter a use case in which you’d like to iterate or loop through a list of JSON objects, especially if there’s information / data that you’d like to run through another request. Instead of hardcoding each request with the required data, we can request the initial list of objects and iterate through it to loop the next request based on the number of objects.

This process requires basic knowledge of inline scripts and the ZT Basic scripting syntax. See https://apica-kb.atlassian.net/wiki/spaces/DAZT/pages/149291571 for more information. The ZT Basic scripting manual pertinent to your ZebraTester version can be found in the “Documentation” subfolder within the root folder of your local ZebraTester installation.

 

Setup

Request 1: (R1) https://api-wpm.apicasystem.com/v3/Help/Route/GET-checks_enabled_severity_include_tag_op

Request 2: (R2) https://api-wpm.apicasystem.com/v3/Help/Route/GET-checks-checkId-metadata

  • Simple GET request that fetches the metadata for an individual check, based on the check ID

Step 1: Record your requests

As with any other ZebraTester script, we need to record / add the requests we want to execute during the test. This requires no particular set up, but we will make some adjustments to make the requests more dynamic for next steps.

Page breaks will be used for looping, but are not necessary as you can do URL loops instead. In this example, we are using Page breaks simply to visualize the different steps / requests.

Optional:

To make the script look and feel more dynamic, we can replace the recorded Check ID on R2 with a variable to understand what we’re replacing in the request. To do this, we can edit the request from the Var Handler:

In the Full Path field, we can change the recorded check ID to something more dynamic for later use (essentially whichever value we will replace later on)

Before:

 

After:

Step 2: Save the R1 content as a JSON

As the JSON response can’t directly be saved as a correct JSON, we need to save it as one using an inline script. We will create two new variables in this inline script:

  • jsonresponse

    • This variable will contain the full JSON response now saved as a correct JSON by adding a “top key” called placeholder.

  • jsonsize

    • This variable will contain an integer which we will later use to determine amount of loops needed

      • This integer is based on the number of objects/items within the placeholder key (in this case, the amount of checks we have available in the response from R1).

This inline script has to be executed after URL 1 in this case, or after the specific URL in your test case.

Code:

jsonresponse = "{\"placeholder\":" + getHTTPResponseContent() + "}" jsonsize = jsonArraySize(jsonresponse,"placeholder")

Step 3: Create iteration variable

Before we move on to iterating through the JSON content, we need to determine which iteration we’re actually on. A simple way of doing this is to have a variable that increases in value for each iteration.

Step 4: Create iteration

Now that we have our complete JSON together with the iteration variable, we can create the logic behind the iteration that will take place on each loop. When we have a correctly formatted JSON object, we can use the inline function “jsonGetData” to get the data we require for the loop, this uses a JSONpath to fetch the data. From our jsonresponse content, we want to fetch every iteration of “id”:

checknumber = jsonGetData(jsonObject,"placeholder."+iteration+".id") iteration = iteration + 1

And for every iteration we add “1” to the iteration variable as preparation for the next iteration; as you can see, we use this variable to extract the correct iteration from the JSON content.

Of course, the JSONpath might differ depending on the structure of content from R1. You can use tools such as to quickly find the path you require.

Step 5: Replace original request with current Check ID

If you performed the optional step in Step 1, you will remember the {CheckID} variable that we created to make the script look more dynamic. This is where we want to replace this value with the checkID that was created from the inline script in Step 4.

This allows us to replace the original value from the request with our generated variable value instead.

The principle is similar if you instead need to replace the value in a POST body or header value, for example. In this particular example, the value is based in the URL.

Step 6: Set your inner loop

In order to actually loop R2 as many times as we have objects in the JSON content, we need to create an inner loop for the 2nd page break.

 

As you remember, the variable “jsonsize” is set to an integer that is based on the amounts of checks/objects we had available in the response from R1. This will define the amount of time we need to loop R2 in order to iterate through all available objects.

The process is now complete! When we execute this script, R1 will execute once, and we will save the response content as a correct JSON. Before executing R2, we will determine the next object to be used in the URL and we will loop this as many times as there are objects in our JSON.

Can't find what you're looking for? Send an E-mail to support@apica.io