Skip to main content
jsonencode(val) serializes val to a JSON string. Maps become objects, lists become arrays, strings, numbers, and booleans are encoded as their JSON equivalents.

Signature

jsonencode(val any) string

Example

Build a deployment metadata payload and pass it to an API endpoint:
variable "app" {
  type    = string
  default = "api"
}

variable "version" {
  type = string
}

variable "env" {
  type    = string
  default = "staging"
}

variable "build_timestamp" {
  description = "RFC 3339 build timestamp"
  type        = string
}

computed "deploy_payload" {
  description = "JSON payload for the deployment API"
  expression  = jsonencode({
    application = var.app
    version     = var.version
    environment = var.env
    deployed_at = var.build_timestamp
  })
}

task "notify-deploy" {
  description = "Record the deployment in the release tracker"
  commands = [
    "curl -X POST https://releases.example.com/api/deploys -H 'Content-Type: application/json' -d '${computed.deploy_payload}'",
  ]
}

Notes

  • The output is compact JSON (no extra spaces or newlines).
  • Map keys are sorted alphabetically in the output.
  • To parse a JSON string back to a value, use jsondecode.