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