Then the API response should contain json:

This step asserts that the most recent API response body contains all of the key-value pairs specified in a JSON doc-string (a Gherkin triple-quoted block). The comparison is partial: only the properties you list need to match. Extra properties in the actual response are ignored, so you can focus on the fields that matter to your test without listing every field the endpoint returns.

The step parses the expected JSON first and throws a clear error if the string is not valid JSON. It then compares the response body property by property using a deep-equal check per key. Any mismatch or missing property fails the step with a message identifying the offending property. Placeholder tokens such as {{userId}} set earlier in the scenario are substituted in the doc-string before parsing, so dynamic values work without manual string building.

Accepted phrasings

Then the API response should contain json:
  """
  { ... }
  """

Then API response should contain json:
  """
  { ... }
  """

Examples

Then the API response should contain json:
  """
  {
    "name": "Jane Smith",
    "email": "jane@example.org"
  }
  """

Then API response should contain json:
  """
  {
    "id": 1,
    "title": "Hello"
  }
  """

Then the API response should contain json:
  """
  {
    "active": true
  }
  """

Then API response should contain json:
  """
  {
    "email": "contact@example.org"
  }
  """

In a real scenario

Scenario: Creating a new user returns the saved data
  Given the API base URL is "https://api.example.org/v1"
  And I set header "Content-Type" with value "application/json"
  When I send a POST request to "/users" with body:
    """
    {
      "name": "Jane Smith",
      "email": "jane@example.org",
      "role": "editor"
    }
    """
  Then the API response code should be 201
  And the response should be valid JSON
  And the API response should contain json:
    """
    {
      "name": "Jane Smith",
      "email": "jane@example.org"
    }
    """

Related steps

Back to Api Steps