github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/job-specification/hcl2/functions/encoding/jsonencode.mdx (about)

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