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
  1. Simulation
  2. Configuring Request Matchers
  3. JSON Request Matchers on the Body

Regular Expression Matcher

Use Hoverfly Cloud's regular expression matcher to match JSON structures in request bodies when other matchers are insufficient.

PreviousJSONPath MatcherNextHandling the response when Hoverfly cannot match

Last updated 3 months ago

You can also use regular expressions if you need to match a particular JSON structure. This is useful if you have various similar request payloads that you cannot discern using JSON, JSON Partial or JSONPath matchers. For example, if you need to match on the existence of a number of certain keys and you do not know their values.

Be aware that the regular expression engine is written in GO and conventions used by that language must be used. In particular it does not support Lookahead or Lookbehind assertions.

Regular expression matching example:

In this example the regular expression will match provided there is a transactionId with a value of 1000, a store, a clientUniqueId and Items. If any are missing it will not match.

This would not be possible with JSONPartial as the values of store, clientUniqueId and items are variable and not known and cannot be specified in a JSON snippet. Only transactionId is known.

JSONPath cannot currently be used if configured through the Hoverfly Cloud editor as it would only be able to return the existence of a single JSON key at a position within the document. Hoverfly does support matcher chaining which will allow you to chain a number of matchers together, however the Hoverfly Cloud editor does not yet support this yet, and if you would prefer to use this method over regular expressions you will need to manually edit the Hoverfly Simulation as described .)

It should be noted that the regular expression can only check for JSON keys that are in the order stated in the matching string. It will not match if they are in a different order in the payload. The GO regexp package used in Hoverfly does not support Lookahead or Lookbehind assertions.

Matcher value

Positive payload

Non match payload

(missing store)

(?s).*("transactionId": 1000).*store.*clientUniqueId.*items.*

{

"someField": "a",

"transactionId": 1000,

"anotherField": "b",

"store": "c",

"clientUniqueId": "12345",

"items": ["item1", "item2", "item3"],

"extraField": "d"

}

{

"someField": "a",

"transactionId": 1000,

"anotherField": "b",

"shop": "c",

"clientUniqueId": "12345",

"items": ["item1", "item2", "item3"],

"extraField": "d"

}

here