Then the response should be valid JSON

This step verifies that the API response body was parsed as a non-null JSON object or array. Because the HTTP client automatically parses responses whose Content-Type is application/json, this step checks whether the resulting data property is a real JavaScript object - not a string, not null, and not a primitive. It is a lightweight smoke-check that belongs near the top of any scenario that goes on to inspect individual fields.

Two phrasings are accepted and are interchangeable. The step does not distinguish between a JSON object and a JSON array - both are considered valid JSON. It fails if no request has been sent yet, or if the body could not be parsed (for example, a plain-text error page returned by a proxy).

Accepted phrasings

Then the response should be valid JSON
Then the API response should be valid JSON

Examples

Then the response should be valid JSON
Then the API response should be valid JSON
And the response should be valid JSON
And the API response should be valid JSON

In a real scenario

Scenario: The articles listing endpoint returns parseable JSON
  Given the API base URL is "https://api.example.org/v1"
  And I set header "Accept" with value "application/json"
  When I send a GET request to "/articles"
  Then the API response code should be 200
  And the response should be valid JSON
  And the response header "Content-Type" should contain "application/json"
  And the JSON response should have property "data"

Related steps

Back to Api Steps