Skip to main content

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, body refers to the request payload helper.
  • The parameterName specifies 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"
}

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]}}
ParameterRequiredDescription
valueYesThe object or array to convert to JSON
keyNoOptional 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:

  1. Convert it to a JSON object using json operator.
  2. Extract a specific property from the header.

Syntax:

{{jwtHeader token [property_key]}}
ParameterRequiredDescription
tokenYesJWT token string (with or without 'Bearer ' prefix)
property_keyNoOptional specific property to extract from the JWT header

Examples:

  1. Extract full JWT header:
{{{json (jwtHeader "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")}}}

Result: {"alg":"HS256","typ":"JWT"}

  1. 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:

{{jwtPayload token [claim_key]}}
ParameterRequiredDescription
tokenYesJWT token string (with or without 'Bearer ' prefix)
claim_keyNoOptional specific claim to extract from the JWT payload

Examples:

  1. Extract full JWT payload:
{{{json (jwtPayload "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")}}}

Result:

{"sub":"1234567890","name":"John Doe","iat":1516239022}
  1. Extract specific claim from JWT payload:
{{jwtPayload "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" "name"}}

Result:

John Doe
  1. 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"
}