Convert JSON Configuration Back to HCL
Learn how to convert JSON Terraform configurations back to human-readable HCL format, including block reconstruction and attribute formatting.
Detailed Explanation
JSON to HCL Conversion
While HCL-to-JSON is straightforward, converting JSON back to HCL requires understanding which JSON keys represent blocks versus attributes. The converter uses Terraform's known block types to make this distinction.
JSON Input
{
"variable": {
"region": {
"description": "AWS region",
"type": "string",
"default": "us-east-1"
}
},
"resource": {
"aws_instance": {
"web": {
"ami": "ami-abc123",
"instance_type": "t3.micro",
"tags": {
"Name": "web-server"
}
}
}
},
"output": {
"instance_id": {
"value": "aws_instance.web.id"
}
}
}
Generated HCL
variable "region" {
description = "AWS region"
type = "string"
default = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-abc123"
instance_type = "t3.micro"
tags = {
Name = "web-server"
}
}
output "instance_id" {
value = "aws_instance.web.id"
}
Block Type Recognition
The converter recognizes Terraform block types: resource, variable, output, data, locals, module, provider, terraform, backend, provisioner, lifecycle, dynamic, content, connection, moved, import, and check.
Attribute Formatting
JSON values are converted back to HCL syntax:
- Strings: double-quoted
- Numbers: unquoted
- Booleans:
true/false - Arrays:
[...]with HCL formatting - Objects:
{...}withkey = valuesyntax - Multi-line strings: heredoc
<<-EOTsyntax
Round-Trip Fidelity
Converting HCL → JSON → HCL preserves the structural meaning but may differ in formatting. Comments are lost during HCL-to-JSON conversion since JSON does not support comments.
Use Case
Converting Terraform JSON configurations (generated by tools or APIs) back to human-readable HCL format. Useful when receiving Terraform configurations from code generation tools, migrating from JSON-based Terraform to standard HCL, or making programmatically generated configs human-editable.