hcl2json
hcl2json copied to clipboard
Returning list instead of object in latest master
Hi,
The output of our file with hcl2json being built from master is the following:
{
"locals": [
{
"example": []
}
]
}
but 0.3.1 produces:
{
"locals": {
"example": [],
}
]
}
This is with the same input:
locals {
example = [
module.example,
module.example2,
]
}
We would expect locals to be an object per 0.3.1.
I believe this is a side-effect of https://github.com/tmccombs/hcl2json/commit/c269182a1356d3e85b1f6b64250c8a4a77cc6364.
What kind of problems is this change causing?
We've implemented a workaround now:
jq 'if .locals | type == "array" then .locals[] else .locals end'
I'm having a similar problem but with resources, for some reason they are arrays with objects inside. Obviously, the resources have different names and I cannot directly apply @dwilliams782's workaround.
e.g.
resource "kafka_acl" "admin_cluster" {
resource_name = "kafka-cluster"
resource_type = "Cluster"
acl_principal = var.admin_principal
acl_operation = "All"
acl_permission_type = "Allow"
acl_host = "*"
}
is converted to:
"admin_cluster": [
{
"acl_host": "*",
"acl_operation": "All",
"acl_permission_type": "Allow",
"acl_principal": "${var.admin_principal}",
"resource_name": "kafka-cluster",
"resource_type": "Cluster"
}
]
It just makes it processing more difficult for us. If it's just a single element under resource what was the reason to define it as array ?
See https://github.com/tmccombs/hcl2json/issues/18
Basically, we can't both be consistent about whether a block produces an object or an array of objects and use a single object if there is only one. Maybe we could have an option to switch between the two behaviors?
Yeah, I see your point @tmccombs. Indeed maybe an option is a way to go.
Meanwhile I'm looking at jq
filtering possibilities to "massage" this data as a workaround.
Is there any way to disable this behavior?
Edit By the way, it appears the issue is more complex. Terraform does not support hcl2json: https://github.com/hashicorp/terraform/issues/9354#issuecomment-512624185
There is not.
If you have a schema available, then https://github.com/hashicorp/hcl/tree/main/cmd/hcldec might be more useful than hcl2json.
If you have a schema available
Since I'm using some terraform providers, I believe I can get it from terraform providers schema -json
, according to https://developer.hashicorp.com/terraform/cli/commands/providers/schema