github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/hcl2/functions/conversion/convert.mdx (about) 1 --- 2 layout: docs 3 page_title: convert - Functions - Configuration Language 4 description: 'The convert function converts a value or an expression to a given type. ' 5 --- 6 7 # `convert` Function 8 9 `convert` converts a value or an expression to a given type. 10 11 Explicit type conversions are rarely necessary in HCL because it will convert 12 types automatically where required. Use the explicit type conversion functions 13 only to normalize types returned in outputs. 14 15 Only numbers and strings containing decimal representations of numbers can be 16 converted to number. All other values will produce an error. 17 18 Only boolean values and the exact strings "true" and "false" can be converted 19 to boolean. All other values will produce an error. 20 21 Only the primitive types (string, number, and bool) can be converted to string. 22 All other values will produce an error. 23 24 `convert(value, type_constraint)` 25 26 ## Examples 27 28 ```shell-session 29 > convert(3, string) 30 "3" 31 > convert("3", number) 32 3 33 > convert("false", bool) 34 false 35 > convert(false, string) 36 "false" 37 > convert(["a", "b", 3], list(string)) 38 [ 39 "a", 40 "b", 41 "3", 42 ] 43 > convert(["c", "b", "b"], set(string)) 44 [ 45 "b", 46 "c", 47 ] 48 > convert({"a" = "foo", "b" = true}, map(string)) 49 { 50 "a" = "foo" 51 "b" = "true" 52 } 53 ```