Miscellaneous Operators
len
The len operator in Beeceptor calculates the length of arrays, strings, or numbers from the request payload. It is helpful for creating mock responses that needs to emit the size of input data.
Syntax
{{len (body 'parameterName')}}
- In the above,
bodyrefers to the request payload helper. - The
parameterNamespecifies the key to evaluate from the request payload/body.
Other examples:
{{len (faker 'string.uuid')}}: Returns 36 as the length of UUIDs.{{len 'Alex'}}: Returns 4 as the size of given string.
Example
Request Payload:
{
"companyName": "Acme Corporation",
"amount": 2342432,
"listOfItems": [
{"name": "one", "description": "details about one"},
{"name": "two", "description": "details about two"},
{"name": "three", "description": "details about three"}
]
}
Mock Template:
{
"itemsCount": {{len (body 'listOfItems')}},
"companyNameLength": {{len (body 'companyName')}},
"amountSize": {{len (body 'amount')}}
}
Response Generated:
{
"itemsCount": 3,
"companyNameLength": 16,
"amountSize": 7
}
object
The object operator creates a JSON object from a list of key-value pairs. This is useful when you need to construct a JSON object dynamically.
Syntax:
{{object key1=value1 key2=value2 ...}}
Example:
{{{json (object name="Ada Lovelace" age=36 city="London") }}}
Result:
{
"name": "Ada Lovelace",
"age": 36,
"city": "London"
}
Note: The triple-stash operator {{{ ... }}} used in the template helps get unescaped HTML, forming a valid JSON object in the response.
array
The array operator lets you create a list of items.
Syntax:
{{array item1 item2 item3}}
Example:
{
"items_numbers": {{{json (array 1 2 3)}}},
"items_string": {{{json (array "One" "Two" "Three")}}}
}
Result:
{
"items_numbers": [1,2,3],
"items_string": ["One","Two","Three"]
}
Note: The triple-stash operator {{{ ... }}} used in the template helps get unescaped HTML, forming a valid JSON array in the response.
json
The json operator converts JavaScript objects and arrays to their JSON string representation. This is especially useful when you need to display object content returned from a different operator.
Syntax:
{{json value [key]}}
| Parameter | Required | Description |
|---|---|---|
value | Yes | The object or array to convert to JSON |
key | No | Optional key to extract from the object before converting to JSON |
Example 1:
{
"nested-key1": {{{json (object nested=(object key1="value1" key2="value2")) 'nested.key1'}}}
}
Result:
{
"nested-key1": "value1"
}
Example 2:
{{ list 'push' 'myList' (object name="Rajesh Parmar" age=30 city="Amsterdam") }}
{{ list 'push' 'myList' (object name="Ada Lovelace" age=36 city="London") }}
{
"myList": [
{{#each (list 'get' 'myList')}}
{{{json this}}}{{#unless @last}},{{/unless}}
{{/each}}
]
}
Result:
{
"myList": [
{"city":"Amsterdam","age":30,"name":"Rajesh Parmar"},
{"city":"London","age":36,"name":"Ada Lovelace"}
]
}
JWT Helpers
jwtHeader
The jwtHeader helper extracts the header portion of a JWT token. You can:
- Convert it to a JSON object using
jsonoperator. - Extract a specific property from the header.
Syntax:
{{jwtHeader token [property_key]}}
| Parameter | Required | Description |
|---|---|---|
token | Yes | JWT token string (with or without 'Bearer ' prefix) |
property_key | No | Optional specific property to extract from the JWT header |
Examples:
- Extract full JWT header:
{{{json (jwtHeader "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")}}}
Result: {"alg":"HS256","typ":"JWT"}
- Extract specific property from JWT header:
{{jwtHeader "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" "alg"}}
Result: HS256
jwtPayload
The jwtPayload helper extracts the payload (claims) portion of a JWT token. You can extract either the entire payload as an object or a specific claim value from the payload.
Syntax:
| Parameter | Required | Description |
|---|---|---|
token | Yes | JWT token string (with or without 'Bearer ' prefix) |
claim_key | No | Optional specific claim to extract from the JWT payload |
Examples:
- Extract full JWT payload:
{{{json (jwtPayload "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")}}}
Result:
{"sub":"1234567890","name":"John Doe","iat":1516239022}
- Extract specific claim from JWT payload:
{{jwtPayload "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" "name"}}
Result:
John Doe
- Extract authorization token from request header and extract sub claim:
{
"sub": "{{ jwtPayload (header 'Authorization') 'sub' }}"
}
Result (if a JWT token is present in the Authorization header):
{
"sub": "1234567890"
}
jsonParse
The jsonParse operator converts a JSON-encoded string into a usable object or array within the template engine. This enables deeper traversal and manipulation of JSON structures passed as strings—especially helpful when the incoming request contains stringified JSON (e.g., from a form, legacy API, or third-party service).
It is the inverse of the json operator, which converts an object or array into a string.
Syntax:
{{jsonParse value [fieldPath]}}
| Parameter | Required | Description |
|---|---|---|
value | Yes | A string that contains a valid JSON array or object |
fieldPath | No | Optional dot-notation path to extract a specific field (e.g., "user.profile.name") |
Use Cases:
- Decode a stringified array to iterate using each
- Extract specific fields from nested JSON structures
- Combine with body, if, or switch to make dynamic decisions from nested structures
- Use inside stateful mocks to interpret stored string values
Examples:
- Basic JSON parsing:
{
"data": "{\"name\":\"John\",\"age\":30}"
}
Template:
Result:
{
"name": "John",
"age": 30
}
- Field extraction:
{
"data": "{\"user\":{\"name\":\"John\",\"profile\":{\"role\":\"admin\",\"level\":5}},\"items\":[{\"id\":1,\"title\":\"First\"},{\"id\":2,\"title\":\"Second\"}]}"
}
Template:
Result:
{
"userName": "John",
"userRole": "admin",
"userLevel": 5,
"firstItemId": 1,
"secondItemTitle": "Second"
}
- Array iteration with field extraction:
{
"data": "[{\"product\": \"Pen\", \"price\": 10}, {\"product\": \"Notebook\", \"price\": 50}]"
}
Template:
Result:
[
{"Product":"Pen","Price":10},
{"Product":"Notebook","Price":50}
]