github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/hcl2/functions/encoding/jsonencode.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: jsonencode - Functions - Configuration Language
     4  description: The jsonencode function encodes a given value as a JSON string.
     5  ---
     6  
     7  # `jsonencode` Function
     8  
     9  `jsonencode` encodes a given value to a string using JSON syntax.
    10  
    11  The JSON encoding is defined in [RFC 7159](https://tools.ietf.org/html/rfc7159).
    12  
    13  This function maps
    14  [Nomad language values](/docs/job-specification/hcl2/expressions#types-and-values)
    15  to JSON values in the following way:
    16  
    17  | Nomad type    | JSON type |
    18  | ------------- | --------- |
    19  | `string`      | String    |
    20  | `number`      | Number    |
    21  | `bool`        | Bool      |
    22  | `list(...)`   | Array     |
    23  | `set(...)`    | Array     |
    24  | `tuple(...)`  | Array     |
    25  | `map(...)`    | Object    |
    26  | `object(...)` | Object    |
    27  | Null value    | `null`    |
    28  
    29  Since the JSON format cannot fully represent all of the Nomad language
    30  types, passing the `jsonencode` result to `jsondecode` will not produce an
    31  identical value, but the automatic type conversion rules mean that this is
    32  rarely a problem in practice.
    33  
    34  ## Examples
    35  
    36  ```shell-session
    37  > jsonencode({"hello"="world"})
    38  {"hello":"world"}
    39  ```
    40  
    41  ## Related Functions
    42  
    43  - [`jsondecode`](/docs/job-specification/hcl2/functions/encoding/jsondecode) performs the opposite operation, _decoding_
    44    a JSON string to obtain its represented value.