How to Convert HCL to JSON for Terraform Review

HCL is the right format for writing Terraform configuration. JSON is sometimes the right format for reviewing, debugging, or processing it. The conversion goes in both directions for different reasons.

Why convert HCL to JSON

Programmatic inspection. Most languages have JSON parsers but not HCL parsers. If you need to write a script that reads Terraform configuration to generate documentation, extract resource information, or validate conventions, JSON is much easier to work with than HCL.

Tool integration. Some infrastructure tooling, linters, and CI pipelines can read JSON but not HCL. Converting the Terraform configuration to JSON lets these tools process it without HCL parsing support.

Debugging complex expressions. HCL supports Terraform functions, conditionals, and dynamic blocks that evaluate to values at plan time. Converting to JSON after evaluation shows the actual resolved values — useful when debugging why a resource is being configured with unexpected values.

Understanding generated configuration. Tools like CDK for Terraform and Pulumi output JSON (.tf.json files). Converting these to HCL makes them more readable for manual review.

What gets lost in HCL to JSON conversion

HCL features that have no JSON equivalent are expanded or lost in conversion:

  • Comments are stripped — HCL supports # and // comments; JSON doesn't
  • Heredoc strings become regular JSON strings with escaped newlines
  • String interpolation (${var.name}) is preserved as a string literal in JSON, not evaluated
  • Formatting — HCL's aligned equals signs and block structure become standard JSON indentation

Converting with Terraform CLI

Terraform itself can output JSON plan data:

# Show current state as JSON
terraform show -json | jq .

# Generate a plan and output as JSON
terraform plan -out=tfplan
terraform show -json tfplan | jq .resources

This doesn't convert the configuration files (.tf) to JSON — it converts the evaluated plan or state, which is more useful for most inspection purposes because it shows resolved values rather than expressions.

For configuration files specifically: Terraform 0.13+ can convert HCL configuration to JSON using terraform fmt -write=false doesn't do this directly, but third-party tools like hcl2json convert .tf files to JSON for inspection.

Converting JSON back to HCL

When working with programmatically generated Terraform JSON (.tf.json), converting to HCL makes it human-readable. The JSON to HCL converter produces equivalent HCL with proper block syntax and formatting — useful when you want to review or modify machine-generated configuration as if it were hand-written.