liveBook · Manning

JSON

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

JSON is a text-based data interchange format that represents structured values and objects.

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

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains configuration object structure, file loading, output shapes, state fields, and comment handling; directs wrapper authors to current field documentation.
Examples
Shows a JSON variable file, a provider alias represented as an array, and a local JSON state file.
What it assumes
Assumes familiarity with Terraform resources and HCL blocks, while defining how their configuration maps to JSON objects.
What you can do after
Build it from scratch as generated .tf.json configuration, or parse Terraform CLI results as single objects or streamed JSONL records.
How it connects
Connects JSON to HCL, input variables, Terraform state, provider configuration, and the machine-readable UI.

Also explained in

Compare how 35 books explain JSON →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.