JSON is a text format for structured data. In Terraform and OpenTofu, it provides a machine-friendly way to supply variable values, generate configuration, inspect state, and read command results. Use HCL for configuration people maintain directly; JSON is especially useful when another program produces or consumes the data. section 6.3.1 section 11.4
The file or command determines what a JSON object means:
- A .tfvars.json file supplies input-variable values. Load it with -var-file, or use terraform.tfvars.json or a .auto.tfvars.json filename for automatic loading. section 5.4.3
- A .tf.json configuration file has top-level keys such as resource, data, variable, and output. Their nested objects replace HCL blocks; JSON and HCL configuration files can coexist in one project. section 11.2.1
- Local terraform.tfstate is JSON. Its top level records state metadata, resources, and outputs; the lineage identifies the project’s state, while the serial advances when state changes. section 6.3.1
Generate a provider configuration
To define both a default AWS provider and a backups alias in JSON, put a provider key at the top level, then an aws key whose value is a list of two objects. The list represents provider blocks that share a name—something an HCL file can express by repeating the block. Save generated Terraform configuration with a .tf.json filename. section 11.2.1 section 11.2
Listing 11.34 provider alias in JSON
{
"provider": {
"aws": [ #1
{ #2
"region": "us-east-1"
},
{
"alias": "backups",
"region": "us-west-2"
}
]
}
}
The first array entry sets the default region; the second sets the backups alias and its region. section 11.2.1
For automation that reads Terraform, -json produces a single result object for commands such as show, validate, and output. Plan and apply instead stream JSONL: one independently parseable JSON object per line. These output structures are versioned, so check current field documentation when building a wrapper. JSON itself has no comments; Terraform configuration can carry explanatory text in a "//" field, but listing annotations must be removed before using copied JSON. section 11.1.1 section 11.2.3 section 11.2.1


































