mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-20 10:19:27 -05:00
Signed-off-by: Janos <86970079+janosdebugs@users.noreply.github.com> Signed-off-by: Damian Stasik <920747+damianstasik@users.noreply.github.com> Signed-off-by: Roman Grinovski <roman.grinovski@gmail.com> Co-authored-by: Damian Stasik <920747+damianstasik@users.noreply.github.com> Co-authored-by: Roman Grinovski <roman.grinovski@gmail.com>
50 lines
1.5 KiB
Plaintext
50 lines
1.5 KiB
Plaintext
---
|
|
sidebar_label: jsonencode
|
|
description: The jsonencode function encodes a given value as a JSON string.
|
|
---
|
|
|
|
# `jsonencode` Function
|
|
|
|
`jsonencode` encodes a given value to a string using JSON syntax.
|
|
|
|
The JSON encoding is defined in [RFC 7159](https://tools.ietf.org/html/rfc7159).
|
|
|
|
This function maps
|
|
[OpenTofu language values](../../language/expressions/types.mdx)
|
|
to JSON values in the following way:
|
|
|
|
| OpenTofu type | JSON type |
|
|
| -------------- | --------- |
|
|
| `string` | String |
|
|
| `number` | Number |
|
|
| `bool` | Bool |
|
|
| `list(...)` | Array |
|
|
| `set(...)` | Array |
|
|
| `tuple(...)` | Array |
|
|
| `map(...)` | Object |
|
|
| `object(...)` | Object |
|
|
| Null value | `null` |
|
|
|
|
Since the JSON format cannot fully represent all of the OpenTofu language
|
|
types, passing the `jsonencode` result to `jsondecode` will not produce an
|
|
identical value, but the automatic type conversion rules mean that this is
|
|
rarely a problem in practice.
|
|
|
|
When encoding strings, this function escapes some characters using
|
|
Unicode escape sequences: replacing `<`, `>`, `&`, `U+2028`, and `U+2029` with
|
|
`\u003c`, `\u003e`, `\u0026`, `\u2028`, and `\u2029`.
|
|
|
|
The `jsonencode` command outputs a minified representation of the input.
|
|
|
|
## Examples
|
|
|
|
```
|
|
> jsonencode({"hello"="world"})
|
|
{"hello":"world"}
|
|
```
|
|
|
|
## Related Functions
|
|
|
|
* [`jsondecode`](../../language/functions/jsondecode.mdx) performs the opposite operation, _decoding_
|
|
a JSON string to obtain its represented value.
|