»yamlencode Function

yamlencode encodes a given value to a string using YAML 1.2 block syntax.

This function maps Nomad language values to YAML tags in the following way:

Nomad typeYAML type
number!!float or !!int
Null value!!null

yamlencode uses the implied syntaxes for all of the above types, so it does not generate explicit YAML tags.

Because the YAML format cannot fully represent all of the Nomad language types, passing the yamlencode result to yamldecode will not produce an identical value, but the Nomad language automatic type conversion rules mean that this is rarely a problem in practice.


> yamlencode({"a":"b", "c":"d"})
"a": "b"
"c": "d"

> yamlencode({"foo":[1, 2, 3], "bar": "baz"})
"bar": "baz"
- 1
- 2
- 3

> yamlencode({"foo":[1, {"a":"b","c":"d"}, 3], "bar": "baz"})
"bar": "baz"
- 1
- "a": "b"
  "c": "d"
- 3

yamlencode always uses YAML's "block style" for mappings and sequences, unless the mapping or sequence is empty. To generate flow-style YAML, use jsonencode instead: YAML flow-style is a superset of JSON syntax.

»Related Functions

  • jsonencode is a similar operation using JSON instead of YAML.
  • yamldecode performs the opposite operation, decoding a YAML string to obtain its represented value.