2018-05-13 19:40:57 +02:00
|
|
|
---
|
2020-08-15 03:51:06 +02:00
|
|
|
layout: "language"
|
2018-12-20 05:35:11 +01:00
|
|
|
page_title: "jsonencode - Functions - Configuration Language"
|
2018-05-13 19:40:57 +02:00
|
|
|
sidebar_current: "docs-funcs-encoding-jsonencode"
|
|
|
|
description: |-
|
|
|
|
The jsonencode function encodes a given value as a JSON string.
|
|
|
|
---
|
|
|
|
|
|
|
|
# `jsonencode` Function
|
|
|
|
|
2019-01-17 01:33:57 +01:00
|
|
|
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
|
|
|
|
earlier, see
|
|
|
|
[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).
|
|
|
|
|
2018-05-13 19:40:57 +02:00
|
|
|
`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).
|
|
|
|
|
2019-03-21 20:20:29 +01:00
|
|
|
This function maps
|
2020-11-13 03:30:52 +01:00
|
|
|
[Terraform language values](/docs/configuration/expressions/types.html)
|
2018-05-13 19:40:57 +02:00
|
|
|
to JSON values in the following way:
|
|
|
|
|
|
|
|
| Terraform 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 Terraform 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.
|
|
|
|
|
2020-09-03 21:21:17 +02:00
|
|
|
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`. This is to preserve
|
|
|
|
compatibility with Terraform 0.11 behavior.
|
|
|
|
|
2018-05-13 19:40:57 +02:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
> jsonencode({"hello"="world"})
|
|
|
|
{"hello":"world"}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Related Functions
|
|
|
|
|
|
|
|
* [`jsondecode`](./jsondecode.html) performs the opposite operation, _decoding_
|
|
|
|
a JSON string to obtain its represented value.
|