JSONPath Count Assertion

Applies to ReadyAPI 3.54, last modified on July 23, 2024

The JSONPath Count assertion uses a JSONPath expression to count the occurrences of an element in a request or response.

The JSONPath expression in this assertion returns a numeric value. To compare a value with some specific content, use the JSONPath Match assertion.

This assertion applies only to requests and responses, bodies of which contain JSON data.

To learn more about the JSONPath syntax, see JSONPath Reference.

Availability

This assertion is available in multiple ReadyAPI applications. Depending on the application, it validates the following data:

In... Checks... To learn more...
Functional tests The request or response body. See Working With Assertions in Functional Tests.
Security tests The response body. See Security Assertions.
ServiceV The request body. See Assertions in Virtual Services.

Create an assertion

Follow these steps:

Functional test: The Assertions panel

Click the image to enlarge it.

  1. Open a test step.

  2. Click Add assertion.

In the New Assertions dialog, search for the JSONPath Count assertion or select it manually in the Property Content category.

Follow these steps:

Security tests: The Assertions panel

Click the image to enlarge it.

  1. Open a security test.

  2. Click Response Assertion next to any scan.

  3. Click in the Assertions panel

In the New Assertions dialog, search for the JSONPath Count assertion or select it manually in the Property Content category.

Follow these steps:

Virtual service: The Assertions panel

Click the image to enlarge it.

  1. Open a virtual service.

  2. Open the Assertions drop-down panel.

  3. Click in the Assertions panel

In the New Assertions dialog, search for the JSONPath Count assertion or select it manually in the Property Content category.

Setting up properties

  1. In the JSONPath Expression edit box, specify the JSONPath expression you want to use to search for elements to count, or use the toolbar controls to create the expression automatically.

    To use the toolbar controls, run the request you want to validate at least once.
    ReadyAPI: Configuring the JSONPath Count assertion

    Click the image to enlarge it.

    Click Select node to call the Select JSON dialog. Use it to select a node from the last request or response and create a JSON path expression for this node.

  2. In the Expected Result edit box, specify the value you are expecting to get.

    Use these commands to visually select the value from the existing request or response:

    Option Description
    Select Content Add a property expansion in the result.
    Select from Current Get a value from the last response you have received (or the last request the virtual service has received).
  3. To command the assertion to use an asterisk wildcard (*) for varying parts of the value, select Allow Wildcards.

    Note: If you do not select this option, the assertion will fail on dynamic values.
  4. To test your assertion on the last response you have received (or the last request the virtual service has received), click Test.

Tips
  • To avoid typing the desired JSONPath expression manually, click Select node and select the desired JSON element or attribute in the subsequent dialog.

    ReadyAPI: Selecting an element for JSONPath expression

    ReadyAPI will form the JSONPath expression that refers the selected item, and will set the result from the last request or response as the expected result.

  • ASP.NET-based services enclose the JSON response payload into the d root element for security reasons. By default, ReadyAPI does not strip the d element out, but there is an option to automatically remove it. If you turn this option on, JSON-related assertions created from the JSON response will not take the root element into account, which can cause the assertion to fail unexpectedly.

  • The following JSONPath expression counts how many text subelements are in the widget element:

    $.widget.text
  • The following JSONPath expression counts all subelements of the widget element:

    $.widget.*
  • The following JSONPath expression gets the size of the elements array:

    $.widget.elements
  • The following JSONPath expression counts all text subelements of the header type in the widget element:

    $.widget.text[?(@.type=='header')]
  • The following JSONPath expression counts all non-empty subelements of the widget element:

    $.widget[?(@!='')]

See Also

Property Content Assertions
JSONPath Existence Match Assertion
JSONPath RegEx Match Assertion
JSONPath Match Assertion
JSONPath Reference

Highlight search results