On occasion, there is a need to transform JSON template data in order to use it with an integration’s endpoint. Use functions to further customize your webhook payloads. The Go template language – and other tools that make use of it – uses template functions and pipelines to make transformation flexible and powerful.
Template functions follow the syntax function arg1 arg2…, and pipelines draw on the UNIX concept of chaining together a series of template commands to express a series of transformations separated by a “pipe” (|) character.
An example combining functions and pipelines together looks like this:
{{ .Environment | upper | quote }}
When evaluated with an Environment of prod, the result will be "PROD".
All the standard Go template actions, functions, and a dozen or so additional template functions are available for use when authoring templates.
date formats a timestamp:
{{ now | date "2006-01-02" }}
The above returns 2024-11-22. Useful in conjunction with .Alert.Timestamp
join joins a list of things into a single string with the provided separator:
{{ list group1 group2 group3 | join "," }}
The above returns group1,group2,group3.
lower converts the entire string to lowercase:
{{ lower "HELLO" }}
The above returns hello.
now returns the current time at the time of template execution. Most useful with date.
quote wraps the string in double quotes:
{{ quote .Environment }}
The above returns "prod" assuming the value of “.Environment” is prod.
sort sorts a list of strings into alphabetical (lexicographical) order:
{{ list orange apple banana | sort }}
The above returns [apple banana orange].
split splits a string into a list of strings:
{{ split ":" "one:two:three" }}
The above returns [one two three].
toJson encodes an item into a JSON string.
If the item cannot be converted to JSON, then the function returns an empty string.
{{ list apple banana orange | toJson }}
The above returns ["apple", "banana", "orange"].
trim removes whitespace from either side of a string:
{{ trim " hello " }}
The above returns hello.
trunc truncates a string by a specified number of characters:
{{ trunc 5 "hello world" }}
The above returns hello.
{{ trunc -5 "hello world" }}
The above returns world.
upper converts the entire string to uppercase:
{{ upper "hello" }}
The above returns HELLO.
duration formats a given amount of seconds in human-readable time:
{{ duration 95 }}
The above returns 1m35s.