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
- ReAPI Jsonata Playground Visit this link to play with Jsonata expressions and examples.
- Jsonata Official Documentation
- Jsonata Official Playground
Basic Jsonata Usage
-
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).
- Dot Notation: Access properties directly with
-
Basic Calculations
- Perform basic arithmetic. For example:
response.data.userId + 1
- This expression adds 1 to the
userId
value.
- Perform basic arithmetic. For example:
-
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)
- String Length: Use
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:
-
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 thatuserId
is a string type, simplifying type-checking assertions.
- In API nodes, Jsonata is commonly used to extract data and define assertions. ReAPI’s UI provides various operators, including Chai.js assertions like
-
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.
- Use Jsonata in If nodes to evaluate conditions and control test flow dynamically. For instance, to check if the user’s role is “admin”:
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.