Skip to main content
jsondecode(str) parses str as JSON and returns the decoded value. JSON objects become typed objects with named attributes, JSON arrays become tuples, strings, numbers, and booleans map to their equivalent types, and null becomes a null value.

Signature

jsondecode(str string) any

Example

Read a JSON configuration file and use its values in task commands:
computed "config" {
  description = "Parsed deployment configuration"
  expression  = jsondecode(file("deploy.json"))
}

task "deploy" {
  description = "Deploy using settings from the config file"
  commands = [
    "kubectl set image deployment/${computed.config["app"]} app=${computed.config["image"]}:${computed.config["tag"]}",
  ]
}
Given deploy.json:
{
  "app": "api",
  "image": "registry.example.com/api",
  "tag": "1.4.2"
}
The command becomes kubectl set image deployment/api app=registry.example.com/api:1.4.2.

Notes

  • If str is not valid JSON, jsondecode reports an error at evaluation time.
  • To produce JSON from a value, use jsonencode.