Skip to content

tm_jsonencode Function

tm_jsonencode encodes a given value to a string using JSON syntax.

The JSON encoding is defined in RFC 7159.

This function maps Terramate language values to JSON values in the following way:

Terramate typeJSON type
stringString
numberNumber
boolBool
list(...)Array
set(...)Array
tuple(...)Array
map(...)Object
object(...)Object
Null valuenull

Since the JSON format cannot fully represent all of the Terramate language types, passing the tm_jsonencode result to tm_jsondecode will not produce an identical value, but the automatic type conversion rules mean that this is rarely a problem in practice.

When encoding strings, this function escapes some characters using Unicode escape sequences: replacing <, >, &, U+2028, and U+2029 with \u003c, \u003e, \u0026, \u2028, and \u2029.

The jsonencode command outputs a minified representation of the input.

Examples

sh
tm_jsonencode({"hello"="world"})
{"hello":"world"}
  • tm_jsondecode performs the opposite operation, decoding a JSON string to obtain its represented value.