JSON to HCL (Terraform)
Terraform accepts JSON, but nobody wants to read it — .tf.json is a machine format that turns a readable config into a wall of braces. This converts a JSON document into idiomatic HCL2: bare identifiers where the key allows it, quoted where it does not, and heredocs for multi-line strings so policy documents and scripts stay legible.
How to use it
- Paste a JSON object. Arrays and scalars work too and are emitted under a
valueattribute. - Choose bare attributes to paste the result inside a block you already have, or wrapped in a block to get a complete
locals { … }you can drop into a file. - Change the block type to
variables,tags, or any resource-argument block you are filling in. - Run
terraform fmton the result. This emits well-formed HCL with two-space indentation, butfmtis the authority on alignment.
What the conversion does and does not do
Keys that match [A-Za-z_][A-Za-z0-9_-]* are emitted bare; anything else is quoted, which HCL2
accepts as an attribute name. Short arrays of scalars stay on one line; longer or nested ones break across
lines. Strings containing a newline become heredocs (<<-EOT), which is the readable form for
an IAM policy, a user-data script or a certificate.
What it cannot do is invent structure. HCL's expressive advantages over JSON — for expressions,
${} interpolation, references to other resources, dynamic blocks — have no equivalent
in the input, so a literal string that happens to contain ${aws_vpc.main.id} is emitted as a
literal string, correctly, and it is on you to decide whether you meant it as an interpolation. Nested objects
become HCL object values, not nested blocks; Terraform accepts both for most arguments, but where a provider
specifically wants a repeated block you will need to reshape by hand.
Privacy
Nothing is uploaded. The conversion runs in this tab using your own browser's JavaScript engine — no server sees your data, and the page keeps working with the network disconnected.
FAQ
Can Terraform just read the JSON directly?
Yes — name the file *.tf.json and Terraform parses it natively. That is the right answer for generated configuration. Convert to HCL when a human is going to maintain the file afterwards.
Does it handle null?
It emits null, which HCL2 understands. In Terraform, setting an argument to null means "unset", which is not the same as an empty string — check that is what you meant.
What about the reverse direction?
Not on this site. terraform show -json and hcl2json both produce JSON from HCL, and their output is a good input to the formatter or jq.
Is the output valid for a variables file?
Bare-attribute output is exactly the shape of a terraform.tfvars file, so yes — save it with that name. A variable block needs type and default declarations, which have to come from you.