Trim

Learn about the Trim OTTL converter function.

The Trim converter removes leading and trailing characters from a string.

Syntax: Trim(target, Optional[replacement])

  • target: the string to trim
  • replacement: an optional string representing the character(s) to remove (default: space character)

Example 1: Trimming whitespace (default)

Input

{
	"_type": "log",
	"body": {
		"trim_test": "  spaced  ",
		"run": 23
	},
	"resource": {...},
	"timestamp": 1727750400000
}

Statement

set(body["trimmed"], Trim(body["trim_test"]))

Output

{
	"_type": "log",
	"body": {
		"trim_test": "  spaced  ",
		"trimmed": "spaced",
		"run": 23
	},
	"resource": {...},
	"timestamp": 1727750400000
}

The function removed leading and trailing spaces from " spaced " resulting in “spaced”.

Example 2: Trimming custom characters

Input

{
	"_type": "log",
	"body": {
		"message": "!!important message!!",
		"run": 24
	},
	"resource": {...},
	"timestamp": 1727750400000
}

Statement

set(body["cleaned"], Trim(body["message"], "!"))

Output

{
	"_type": "log",
	"body": {
		"message": "!!important message!!",
		"cleaned": "important message",
		"run": 24
	},
	"resource": {...},
	"timestamp": 1727750400000
}

The function removed leading and trailing exclamation marks from “!!important message!!” resulting in “important message”.