2018-05-13 19:40:57 +02:00
|
|
|
---
|
|
|
|
layout: "functions"
|
|
|
|
page_title: "jsonencode function"
|
|
|
|
sidebar_current: "docs-funcs-encoding-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 fucntion maps
|
2018-12-11 01:14:33 +01:00
|
|
|
[Terraform language values](./expressions.html#types-and-values)
|
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.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
> jsonencode({"hello"="world"})
|
|
|
|
{"hello":"world"}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Related Functions
|
|
|
|
|
|
|
|
* [`jsondecode`](./jsondecode.html) performs the opposite operation, _decoding_
|
|
|
|
a JSON string to obtain its represented value.
|