github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/hcl2/functions/encoding/yamlencode.mdx (about) 1 --- 2 layout: docs 3 page_title: yamlencode - Functions - Configuration Language 4 description: The yamlencode function encodes a given value as a YAML string. 5 --- 6 7 # `yamlencode` Function 8 9 `yamlencode` encodes a given value to a string using 10 [YAML 1.2](https://yaml.org/spec/1.2/spec) block syntax. 11 12 ~> **Warning:** This function is currently **experimental** and its exact 13 result format may change in future versions of Nomad, based on feedback. 14 Do not use `yamldecode` to construct a value for any argument where 15 changes to the result would be disruptive. To get a consistent string 16 representation of a value use [`jsonencode`](/docs/job-specification/hcl2/functions/encoding/jsonencode) instead; its 17 results are also valid YAML because YAML is a JSON superset. 18 19 <!-- 20 The condition for removing the above warning is that the underlying 21 go-cty-yaml folder makes a stable release with a commitment to guarantee 22 that the representation of particular input will not change without a 23 major release. It is not making that commitment at the time of writing to 24 allow for responding to user feedback about its output format, since YAML 25 is a very flexible format and its initial decisions may prove to be 26 sub-optimal when generating YAML intended for specific external consumers. 27 --> 28 29 This function maps 30 [Nomad language values](/docs/job-specification/hcl2/expressions#types-and-values) 31 to YAML tags in the following way: 32 33 | Nomad type | YAML type | 34 | ------------- | -------------------- | 35 | `string` | `!!str` | 36 | `number` | `!!float` or `!!int` | 37 | `bool` | `!!bool` | 38 | `list(...)` | `!!seq` | 39 | `set(...)` | `!!seq` | 40 | `tuple(...)` | `!!seq` | 41 | `map(...)` | `!!map` | 42 | `object(...)` | `!!map` | 43 | Null value | `!!null` | 44 45 `yamlencode` uses the implied syntaxes for all of the above types, so it does 46 not generate explicit YAML tags. 47 48 Because the YAML format cannot fully represent all of the Nomad language 49 types, passing the `yamlencode` result to `yamldecode` will not produce an 50 identical value, but the Nomad language automatic type conversion rules 51 mean that this is rarely a problem in practice. 52 53 ## Examples 54 55 ```shell-session 56 > yamlencode({"a":"b", "c":"d"}) 57 "a": "b" 58 "c": "d" 59 60 > yamlencode({"foo":[1, 2, 3], "bar": "baz"}) 61 "bar": "baz" 62 "foo": 63 - 1 64 - 2 65 - 3 66 67 > yamlencode({"foo":[1, {"a":"b","c":"d"}, 3], "bar": "baz"}) 68 "bar": "baz" 69 "foo": 70 - 1 71 - "a": "b" 72 "c": "d" 73 - 3 74 ``` 75 76 `yamlencode` always uses YAML's "block style" for mappings and sequences, unless 77 the mapping or sequence is empty. To generate flow-style YAML, use 78 [`jsonencode`](/docs/job-specification/hcl2/functions/encoding/jsonencode) instead: YAML flow-style is a superset 79 of JSON syntax. 80 81 ## Related Functions 82 83 - [`jsonencode`](/docs/job-specification/hcl2/functions/encoding/jsonencode) is a similar operation using JSON instead 84 of YAML. 85 - [`yamldecode`](/docs/job-specification/hcl2/functions/encoding/yamldecode) performs the opposite operation, _decoding_ 86 a YAML string to obtain its represented value.