IBM Support

Implement JSON template files as input variables for local variables or in for loop

How To


Steps

Introduction

This guide demonstrates two methods for using JSON data with Terraform. You can either use a static JSON file as an input variable or dynamically generate a JSON file from a list using a template and a for loop.

Procedure

Follow the steps for the method that best fits your use case.

Method 1: Use a JSON file as an input variable

This method is suitable when you have a complete JSON file that you want to use for variable values.

  1. Create a main.tf file that defines an input variable for the JSON file path and uses the jsondecode and file functions to parse its content.

    variable "json_input" {
      description = "Path to the JSON input file"
      type        = string
    }
    
    locals {
      input_data = jsondecode(file(var.json_input))
    }
    
    output "name" {
      value = local.input_data.name
    }
  2. Create a JSON file named test.json with the data you want to use.

    {
      "name": "testname",
      "age": 100,
      "city": "San Francisco"
    }
  3. Run terraform apply and pass the path to your JSON file using the -var flag.

    $ terraform apply -var="json_input=test.json"

    Terraform will read the file, parse the JSON content, and use the values as defined.

Method 2: Generate a JSON file using a template and a for loop

This method is useful when you need to construct a JSON file dynamically from a Terraform list variable.

  1. Create a main.tf file. This configuration defines a list of strings, then uses the template_file data source with a for loop and the jsonencode function to create a JSON structure. Finally, it saves the result to a local file.

    # Define a list of items
    variable "items" {
      type    = list(string)
      default = ["Rahul", "Test1", "Test2"]
    }
    
    # Render the template
    data "template_file" "items_template" {
      template = file("template.tpl")
    
      vars = {
        items_json = jsonencode([for item in var.items : { name = item }])
      }
    }
    
    # Create a local file to save the generated JSON
    resource "local_file" "items_json" {
      filename = "items.json"
      content  = data.template_file.items_template.rendered
    }
  2. Create a template file named template.tpl.

    {
      "items": ${items_json}
    }
  3. Run terraform apply to generate the items.json file.

    $ terraform apply

    This process iterates over the var.items list, creates a JSON array of objects, and writes it to the items.json file.

 

Document Location

Worldwide

[{"Type":"MASTER","Line of Business":{"code":"LOB77","label":"Automation Platform"},"Business Unit":{"code":"BU048","label":"IBM Software"},"Product":{"code":"SSGH5YK","label":"IBM Terraform Self-Managed"},"ARM Category":[{"code":"","label":""}],"ARM Case Number":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Version(s)"}]

Historical Number

30838846441747

Document Information

Modified date:
16 March 2026

UID

ibm17265542