Hoverfly Cloud
Visit Our Main SitePricingTry Hoverfly Cloud
  • Introduction
  • Use cases for API Simulation
  • Key concepts
  • What's New
  • Dashboard
  • Simulation
    • Create simulations
    • Configuring Request Matchers
      • JSON Request Matchers on the Body
        • EXACT Matcher
        • JSON Matcher
        • JSON Partial Matcher
        • JSONPath Matcher
        • Regular Expression Matcher
      • Handling the response when Hoverfly cannot match
    • Building a JSON Response
      • Using a JSON object from the Request
      • Looping over a JSON array from the Request
    • Simulating Webhooks and Callbacks
    • Using templating in simulations
      • Render back any part of the Request
      • Render synthetic data
      • Faker functions
      • Render dates and times with offsets
      • Conditional logic
      • Looping over arrays
      • Variables and Arrays
      • Arithmetic operations
      • String operations
      • Validation operations
      • Simulating a persistent backend
      • Transitioning state between requests
      • Combining and nesting templating functions
      • Useful helper functions
      • Avoiding HTML encoding in function return values
    • Using data sources in simulations
      • Querying data sources
      • Updating data in data sources
      • Deleting data from data sources
      • Inserting data into a data source
      • Guidance on using the SQL like syntax
    • Working with Simulation Files
  • Service
    • Start a new service
    • Use a service
    • Update a service
    • Configuring Journal Indexing
  • Command line interface
    • Hoverfly Cloud CLI commands
  • Tutorials
    • Quickstart
    • Creating simulations and services
      • Tutorial 1: Create a service and simulation manually
      • Tutorial 2: Create a service and simulation by importing from a Swagger specification
      • Tutorial 3: Create a service and simulation by capturing real API traffic
    • Response Templating
      • Tutorial 4: Response Templating
    • Hoverfly service modes
      • Tutorial 5: Simulate, Capture, Spy and Passthrough modes
    • Automating with the CLI and API
      • Tutorial 6: Using the CLI and the Hoverfly Cloud API
Powered by GitBook
On this page
  • Example 1:
  • Example 2:
  1. Simulation
  2. Using templating in simulations

Looping over arrays

Learn how to loop over arrays in Hoverfly Cloud simulations using templating to dynamically generate responses.

The ability to loop over arrays of data can be very useful in constructing a response. Some of the arrays typically in a response are:

  • the elements of path

  • the headers in the request

  • an array of query parameters if there are more than one with the same name

  • a JSON array extracted from the body of a request

The following block is used to loop over an array:

  • {{#each}} {{else}} {{/each}}

You can use the @index, @key and this keywords to determine the index, key and value of the object under the iterator. You can use @last keyword to determine if the current object is the last one in the array.

Example 1:

{{#each Request.Header}}
	{{@index}} : {{@key}} : {{this}}
{{else}}
	No Request Headers were provided
{{/each}}

Example 2:

Using looping and conditional logic you can build our a JSON (or XML) response:

Given the following request body

{
    "people": [
        {
            "name": "John",
            "surname": "Doe",
            "age": 30
        },
        {
            "name": "Jane",
            "surname": "Lee",
            "age": 29
        }
    ]
}

And creating the following simulation body template

{ "ages":
    [
        {{#each (Request.Body 'JSONpath' '$.people') }}
        {{this.age}}
        {{#unless @last}},{{/unless}}
        {{/each}}
    ]
}

The result generated from the template and sent back in the body would be this:

{
    "ages": [
        30,
        29
    ]
}
PreviousConditional logicNextVariables and Arrays

Last updated 3 months ago

Follow a tutorial to learn more about looping

here