Convert Hashicorp Configuration Language V2 (HCL2) to YAML/JSON

Sample Python Source

The following sample Python source accepts a path to HCL2 file as an input parameter, and prints its contents as YAML and JSON:

import sys, hcl2, yaml, json

input_tf_file = sys.argv[1]

with open(input_tf_file, 'r') as file:
    tf_file = hcl2.load(file)

tf_yaml_fmt = yaml.safe_dump(tf_file,default_flow_style=False)
print("YAML representation of input file " + input_tf_file + "=>")
print(tf_yaml_fmt)

tf_json_fmt = json.dumps(tf_file, indent=4)
print("JSON representation of input file " + input_tf_file + "=>")
print(tf_json_fmt)

Usage

To generate YAML and JSON representations of an input file with name main.tf and following sample contents,

resource "github_organization_ruleset" "example" {
  name        = "example"
  target      = "branch"
  enforcement = "active"

  conditions {
    ref_name {
      include = ["~ALL"]
      exclude = []
    }
  }

  bypass_actors {
    actor_id    = 13473
    actor_type  = "Integration"
    bypass_mode = "always"
  }

  rules {
    creation                = true
    update                  = true
    deletion                = true
    required_linear_history = true
    required_signatures     = true

    branch_name_pattern {
      name     = "example"
      negate   = false
      operator = "starts_with"
      pattern  = "ex"
    }
  }
}

run the following:

$ python3 hcl2_to_yaml_json.py main.tf

output:

YAML representation of input file main.tf=>
resource:
- github_organization_ruleset:
    example:
      bypass_actors:
      - actor_id: 13473
        actor_type: Integration
        bypass_mode: always
      conditions:
      - ref_name:
        - exclude: []
          include:
          - ~ALL
      enforcement: active
      name: example
      rules:
      - branch_name_pattern:
        - name: example
          negate: false
          operator: starts_with
          pattern: ex
        creation: true
        deletion: true
        required_linear_history: true
        required_signatures: true
        update: true
      target: branch

JSON representation of input file main.tf=>
{
    "resource": [
        {
            "github_organization_ruleset": {
                "example": {
                    "name": "example",
                    "target": "branch",
                    "enforcement": "active",
                    "conditions": [
                        {
                            "ref_name": [
                                {
                                    "include": [
                                        "~ALL"
                                    ],
                                    "exclude": []
                                }
                            ]
                        }
                    ],
                    "bypass_actors": [
                        {
                            "actor_id": 13473,
                            "actor_type": "Integration",
                            "bypass_mode": "always"
                        }
                    ],
                    "rules": [
                        {
                            "creation": true,
                            "update": true,
                            "deletion": true,
                            "required_linear_history": true,
                            "required_signatures": true,
                            "branch_name_pattern": [
                                {
                                    "name": "example",
                                    "negate": false,
                                    "operator": "starts_with",
                                    "pattern": "ex"
                                }
                            ]
                        }
                    ]
                }
            }
        }
    ]
}