JSON to Terraform HCL Variables Converter — Free Online
Convert JSON to Terraform HCL variable blocks and locals definitions using AI. Maps JSON types to Terraform types automatically. Free, no sign-up required.
JSON to Terraform HCL Variables Converter — Free Online
About JSON to Terraform HCL Variables Converter — Free Online
JSON to Terraform HCL converts JSON objects into Terraform variable declarations and locals blocks, mapping JSON types to their HCL equivalents so infrastructure configuration can be expressed as code. Developers use it to migrate existing JSON-based configs, API responses, or CloudFormation templates into Terraform-compatible HCL without manually rewriting every field.
JSON to HCL Type Mapping
| JSON Type | HCL Type | Example Input | Example Output |
|---|---|---|---|
| string | string | "region": "us-east-1" | region = "us-east-1" |
| number (integer) | number | "port": 8080 | port = 8080 |
| number (float) | number | "threshold": 0.75 | threshold = 0.75 |
| boolean | bool | "enabled": true | enabled = true |
| null | null | "value": null | value = null |
| object | object / map(any) | "tags": {"env": "prod"} | tags = { env = "prod" } |
| array of strings | list(string) | "cidrs": ["10.0.0.0/8"] | cidrs = ["10.0.0.0/8"] |
| array of objects | list(object(...)) | "rules": [{"port": 80}] | rules = [{ port = 80 }] |
| nested object | object(...) or any | "network": {"vpc": {"id": "vpc-1"}} | network = { vpc = { id = "vpc-1" } } |
HCL Variable Block vs Locals Block
| Attribute | variable block | locals block |
|---|---|---|
| Purpose | Accepts input from caller (CLI, tfvars, env) | Defines internal computed or static values |
| Syntax | variable "name" { default = value } | locals { name = value } |
| Override at runtime | Yes — via -var, .tfvars, TF_VAR_* | No — computed once during plan/apply |
| Type constraint | Optional type = string/number/bool/list/map | Not supported; type is inferred |
| Validation block | Supported (condition + error_message) | Not supported |
| Reference syntax | var.name | local.name |
| Best for | Environment-specific values, secrets, toggles | Derived values, tag maps, repeated expressions |
| Sensitive flag | sensitive = true hides value in output | mark with sensitive() function |
Frequently Asked Questions
How do I convert a JSON object to a Terraform locals block?
Wrap each top-level JSON key as an assignment inside a locals { } block and replace JSON colons with equals signs, remove double quotes from keys, and drop commas. For example, {"region": "us-east-1", "count": 3} becomes locals { region = "us-east-1" count = 3 }. Nested objects become inline HCL maps using curly braces, and arrays become HCL tuples using square brackets.
What is the difference between a Terraform variable and a local in HCL?
A variable block declares an input that callers supply at runtime via -var flags, .tfvars files, or TF_VAR_ environment variables, making it suitable for environment-specific values like region or instance type. A locals block defines values that are computed once during plan/apply and cannot be overridden externally, making it better for derived expressions, repeated tag maps, or intermediate calculations. Reference variables with var.name and locals with local.name.
Can I use JSON directly in Terraform instead of converting to HCL?
Yes. Terraform natively parses .tf.json files that use JSON syntax matching the HCL structure, so you can write your entire configuration in JSON. However, most teams prefer HCL for its readability, comments support, and string interpolation syntax. The jsondecode() built-in function also lets you parse a JSON string at runtime inside an expression, for example to read a JSON file with file() and decode it inline.
How does Terraform handle JSON null values when converted to HCL?
JSON null maps directly to the HCL literal null. When used as a variable default (default = null) it signals that the variable has no default and must be supplied, unless the type is marked optional. In a locals block or resource argument, null tells Terraform to omit the attribute, which causes the provider to use its own default for that field.
What Terraform HCL types do JSON arrays map to?
A JSON array maps to an HCL tuple when elements are mixed types, or to a typed list/set when all elements share the same type — for instance list(string) for an array of strings or list(object({...})) for an array of uniform objects. In variable type constraints you should specify the most specific type possible (list(string), set(number)) so Terraform can validate inputs at plan time. For locals, the type is inferred automatically from the assigned value.
If jsondecode.com saved you time, share it with your team
Free forever. No ads. No sign-up. Help other developers find it.