• HashiCorp Developer

  • HashiCorp Cloud Platform
  • Terraform
  • Packer
  • Consul
  • Vault
  • Boundary
  • Nomad
  • Waypoint
  • Vagrant
Terraform
  • Install
  • Tutorials
    • About the Docs
    • Configuration Language
    • Terraform CLI
    • Terraform Cloud
    • Terraform Enterprise
    • CDK for Terraform
    • Provider Use
    • Plugin Development
    • Registry Publishing
    • Integration Program
  • Registry(opens in new tab)
  • Try Cloud(opens in new tab)
  • Sign up
Terraform Home

Configuration Language

Skip to main content
  • Configuration Language
  • Data Sources
    • Overview
      • base64decode
      • base64encode
      • base64gzip
      • csvdecode
      • jsondecode
      • jsonencode
      • textdecodebase64
      • textencodebase64
      • urlencode
      • yamldecode
      • yamlencode
  • Upgrading to Terraform v1.3
  • v1.x Compatibility Promises

  • Terraform Internals

  • Resources

  • Tutorial Library
  • Certifications
  • Community Forum
    (opens in new tab)
  • Support
    (opens in new tab)
  • GitHub
    (opens in new tab)
  • Terraform Registry
    (opens in new tab)
  1. Developer
  2. Terraform
  3. Configuration Language
  4. Functions
  5. jsonencode
  • Terraform
  • v1.2.x
  • v1.1 and earlier

»jsonencode Function

jsonencode encodes a given value to a string using JSON syntax.

The JSON encoding is defined in RFC 7159.

This function maps Terraform language values to JSON values in the following way:

Terraform typeJSON type
stringString
numberNumber
boolBool
list(...)Array
set(...)Array
tuple(...)Array
map(...)Object
object(...)Object
Null valuenull

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.

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.

The jsonencode command outputs a minified representation of the input.

Examples

> jsonencode({"hello"="world"})
{"hello":"world"}

Related Functions

  • jsondecode performs the opposite operation, decoding a JSON string to obtain its represented value.
Edit this page on GitHub

On this page

  1. jsonencode Function
  2. Examples
  3. Related Functions
Give Feedback(opens in new tab)
  • Certifications
  • System Status
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
  • Trade Controls
  • Give Feedback(opens in new tab)