hcl2json icon indicating copy to clipboard operation
hcl2json copied to clipboard

Returning list instead of object in latest master

Open dwilliams782 opened this issue 4 years ago • 8 comments

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.

dwilliams782 avatar Oct 09 '20 11:10 dwilliams782

I believe this is a side-effect of https://github.com/tmccombs/hcl2json/commit/c269182a1356d3e85b1f6b64250c8a4a77cc6364.

What kind of problems is this change causing?

tmccombs avatar Oct 09 '20 20:10 tmccombs

We've implemented a workaround now:

jq 'if .locals | type == "array" then .locals[] else .locals end'

dwilliams782 avatar Oct 12 '20 12:10 dwilliams782

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 ?

Constantin07 avatar Jan 26 '22 15:01 Constantin07

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?

tmccombs avatar Jan 26 '22 16:01 tmccombs

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.

Constantin07 avatar Jan 26 '22 20:01 Constantin07

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

giuliohome avatar Sep 23 '23 19:09 giuliohome

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.

tmccombs avatar Sep 25 '23 04:09 tmccombs

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

giuliohome avatar Sep 25 '23 06:09 giuliohome