Jsonata Expression

Jsonata is a powerful expression language for querying and transforming JSON data, widely used in ReAPI for dynamic data extraction, transformations, and condition evaluations. Here are resources and usage tips to help you get started:

Resources

Basic Jsonata Usage

  1. Accessing Properties

    • Dot Notation: Access properties directly with . (e.g., response.data.userId).
    • Bracket Notation: Access array elements by index (e.g., response.data.items[0] for the first item).
  2. Basic Calculations

    • Perform basic arithmetic. For example:
      response.data.userId + 1
    • This expression adds 1 to the userId value.
  3. Basic Functions

    • String Length: Use $length() to find the length of a string.
      $length(response.data.username)
    • Array Length: Use $count() to get the number of items in an array.
      $count(response.data.items)

Jsonata Usage in ReAPI Nodes

Jsonata expressions are applied throughout ReAPI nodes to enhance test logic, control flows, and data extraction. Here are key use cases:

  1. Assertions and Data Extraction in API Nodes

    • In API nodes, Jsonata is commonly used to extract data and define assertions. ReAPI’s UI provides various operators, including Chai.js assertions like .equal, .include, and .isString, which can be selected to validate data directly.
    • Example: To check if userId is a string, you can set up an assertion using:
      response.data.userId
    • In the UI, you can select isString to confirm that userId is a string type, simplifying type-checking assertions.
  2. Evaluating Conditions in If Nodes

    • Use Jsonata in If nodes to evaluate conditions and control test flow dynamically. For instance, to check if the user’s role is “admin”:
      response.data.role = "admin"
    • This allows conditional branching, guiding the test flow based on whether the condition is met.

With Jsonata and ReAPI’s UI operators, you can easily manage data extraction, create robust assertions, and control test flows, making ReAPI tests adaptable and data-driven. This guide equips you with foundational skills to make the most of Jsonata in your test cases.