Skip to main content

JSON Asserters

BotiumScript offers various assertion types to validate chatbot responses, ensuring that they meet expected criteria. This article will cover two essential JSON asserters: JSON_PATH and JSON_PATH_COUNT. Each section will provide a detailed explanation, usage examples, and configuration options.

Tip: The structure of the data depends on the nature of the connector used – for example, with IBM Watson, the underlying response data is the API response from the Watson HTTP/JSON API.

Generic JSONPATH

This JSON_PATH asserter allows you to validate the presence and value of elements within a JSON structure returned by the chatbot. It is particularly useful for checking if specific data is included in the bot's response.

Imagine an eCommerce chatbot where the response contains the shopping cart in session variables. The following BotiumScript asserts that the cart is available in the session, and the ordered item is in the cart:

Botium Script Example:
#me
add to cart 5 bananas

#bot
JSON_PATH $.session.cart
JSON_PATH $.session.cart.item[0].name | banana
Explanation
  1. User Action: The user sends a command to the chatbot to add 5 bananas to the cart.
  2. Bot Response: The chatbot should update the session with the cart details.
JSON_PATH Assertions
The JSON_PATH asserter takes one or two arguments:
  1. First Argument: The JSONPath expression to query.
  2. Second Argument (Optional): If provided, the value is compared to the outcome of the JSONPath expression. If the expression results in multiple values, it is compared to all of them. If not provided, only the existence of the element is asserted.
Note: This asserter always works on the sourceData field of the botMsg, not on the botMsg as a whole.

Generic JSONPATH COUNT

This asserter will validate the number of JSONPath results. You can use number comparisons or specify a number for equality:

  • JSON_PATH_COUNT $.session.cart.item|2
  • JSON_PATH_COUNT $.session.cart.item|=2
  • JSON_PATH_COUNT $.session.cart.item|>2
  • JSON_PATH_COUNT $.session.cart.item|<=3

Extending JSONPath Asserters

The JSONPath Asserter can optionally be configured with global arguments in botium.json. Arguments from the convo file are handed over and used as specified.

  • argCount: Number of arguments to expect in the convo file.
  • path: Predefined JSONPath expression.
  • pathTemplate: Mustache template for predefined JSONPath expression (based on args).
  • assertTemplate: Mustache template for assertion value (based on args).
  • matchingMode (since 1.11.6): Matching mode to use for assertions (default is to use the global matching mode).

Example 1 - WATSONV1_HAS_CONTEX

This example checks if a context variable exists in the response.
{
  "botium": {
    "Capabilities": {
      "ASSERTERS": [
        {
          "ref": "WATSONV1_HAS_CONTEXT",
          "src": "JsonPathAsserter",
          "args": {
            "argCount": 1,
            "pathTemplate": "$.context['{{args.0}}']",
            "matchingMode": "equalsIgnoreCase"
          }
        }
      ]
    }
  }
}
Usage:
#bot
WATSONV1_HAS_CONTEXT my-context-variable

Example 2 - WATSONV1_CONTEXT

This example checks if a context variable has a specific value.
{
  "botium": {
    "Capabilities": {
      "ASSERTERS": [
        {
          "ref": "WATSONV1_CONTEXT",
          "src": "JsonPathAsserter",
          "args": {
            "argCount": 2,
            "pathTemplate": "$.context['{{args.0}}']",
            "assertTemplate": "{{args.1}}"
          }
        }
      ]
    }
  }
}
Usage:
#bot
WATSONV1_CONTEXT my-context-variable|expected-value

Was this article helpful?

0 out of 0 found this helpful