Comparison & Set Operators
You can use comparison operators within an if/else block to create dynamic responses based on incoming request parameters. In Beeceptor, comparison expressions follow a prefix-based notation, where the operator is placed before the arguments. Refer to the supported operators and their usage to craft dynamic mock responses.
Note:
- By default, template engine is off. You need to explicitly mark a rule to enable usage of operators and If/Else constructs.
- If you have a use-case for an operator that isn't listed below, please contact our Support Team. They can assist you in prioritizing your request.
Comparison Operators
eq (equals compare)
The eq
(equals) operator checks if two arguments are equal, returning a boolean value. It supports both number and string data types and returns false if the types or the values don't match.
The following example demonstrates how to use the eq
operator to return a specific response when both arguments are the same.
{{#if (eq 100 100)}}
This text block is always qualified for the response.
{{/if}}
You can also use the eq
operator to conditionally send a response based on the value of a field in the request body. In the example below, the response block varies based on the value of stage
field in the request body.
{{#if (eq (body 'stage') 1)}}
{
"message": "This JSON is qualified for the response if the stage field is sent as 1."
}
{{else}}
{
"message": "This JSON is sent if the stage field is not 1."
}
{{/if}}
Explore more operators and their usage to craft flexible and dynamic mock responses that fit your specific needs.
gt (greater than)
The gt
(greater than) operator checks if the first argument is greater than the second, returning a boolean value.
The example below shows how to use the gt
operator to send a conditional response based on the creditScore
field in the request body. If the creditScore
exceeds 700, the application status is marked as "APPROVED".
{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (gt (body 'creditScore') 700 )}}
"status": "APPROVED",
{{else}}
"status": "REJECTED",
{{/if}}
}
lt (less than)
The lt
(less than) operator checks if the first argument is lesser than the second, returning a boolean value.
The example below shows how to use the lt
operator to send a conditional response based on the creditScore
field in the request body. If the creditScore
is less than 700, the application status is marked as "REJECTED."
{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (lt (body 'creditScore') 700)}}
"status": "REJECTED",
{{else}}
"status": "APPROVED",
{{/if}}
}
lte (less than or equal to)
The lte
(less than or equal to) operator checks if the first argument is less than or equal to the second, returning a boolean value.
The example below shows how to use the lte
operator to send a conditional response based on the creditScore
field in the request body. If the creditScore
is less than or equal to 700, the application status is marked as "REVIEW."
{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (lte (body 'creditScore') 700)}}
"status": "REVIEW",
{{else}}
"status": "APPROVED",
{{/if}}
}
gte (greater than or equal to)
The gte
(greater than or equal to) operator checks if the first argument is greater than or equal to the second, returning a boolean value.
The example below shows how to use the gte
operator to send a conditional response based on the creditScore
field in the request body. If the creditScore
is greater than or equal to 700, the application status is marked as "APPROVED."
{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (gte (body 'creditScore') 700)}}
"status": "APPROVED",
{{else}}
"status": "REJECTED",
{{/if}}
}
Set Operators
sort
The sort
operator sorts an array of values in ascending order by default. You can also specify the sorting order as asc
or desc
to sort the array in ascending or descending order, respectively.
Syntax:
{{sort array_expression sort_order}}
Template definition:
{
"num-list-asc": [{{sort (array 5 2 8 1 3) 'asc'}}],
"str-list-desc": "{{sort (array 'lion' 'zebra' 'aligator' 'elephant') 'desc'}}"
}
Generated response:
{
"num-list-asc": [1,2,3,5,8],
"str-list-desc": "zebra,lion,elephant,aligator"
}
oneOf (random pick)
The oneOf
operator selects a random item from a provided list of values. You can supply any number of values, and this operator will pick one randomly. If no values are provided, it returns an empty string.
In the example below, the operator picks a random status from a list of possible application statuses.
Template definition:
{
"status" : "{{oneOf 'draft' 'in-progress' 'approved' 'rejected' 'cancelled'}}"
}
Generated response:
{
"status": "cancelled"
}
Demo:
someOf (random pick)
The someOf
operator selects a random subset of items from a provided list of values. You can specify the number of items to pick from the list.
Syntax:
{{someOf array_expression min_items max_items stringify}}
Parameter | Required | Default | Description |
---|---|---|---|
array_expression | Yes | - | An array subexpression of values to pick from |
min_items | No | 0 | Minimum number of items to pick |
max_items | No | array_expression.length | Maximum number of items to pick |
stringify | No | false | If true, the output will be a valid JSON array, use {{{}}} to escape |
Example template definition:
{
"owns-list": {{{someOf (array 'car' 'house' 'boat' 'bike' 'land') 1 2 true}}},
"owns-string": "{{someOf (array 'car' 'house' 'boat' 'bike' 'land') 3}}"
}
Example generated response:
{
"owns-list": ["bike","land"],
"owns-string": "land,bike,boat,house"
}