Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion of HCL to/from JSON #35

Open
nj2208 opened this issue Mar 22, 2023 · 3 comments
Open

Conversion of HCL to/from JSON #35

nj2208 opened this issue Mar 22, 2023 · 3 comments
Labels

Comments

@nj2208
Copy link

nj2208 commented Mar 22, 2023

Hi

Have one query regarding conversion of HCL to/from JSON . Conversion from HCL to JSON seems fine however while converting back the generated json to HCL it is not same . As seen in the example below docker_ports was single element of type list / array when converted to json whereas when it is converted back to HCL from from json , docker_ports is represented as two elements instead of two .Any idea why it was not exactly same as orignal HCL after conversion? Any way to get back same HCL format ?

Original tfvars file

image_id = "ami-123"

cluster_min_nodes = 2

cluster_decimal_nodes = 2.2

cluster_max_nodes = true

availability_zone_names = [
  "us-east-1a",
  "us-west-1c",
]

docker_ports = [{
  internal = 8300
  external = 8300
  protocol = "tcp"
},
{
  internal = 8301
  external = 8301
  protocol = "tcp"
}
] 

Conversion of HCL to json

$ ./yj-linux-amd64 -cj < testing.tfvars

{
  "image_id": "ami-123",
  "cluster_min_nodes": 2,
  "cluster_decimal_nodes": 2.2,
  "cluster_max_nodes": true,
  "availability_zone_names": [
    "us-east-1a",
    "us-west-1c"
  ],
  "docker_ports": [
    {
      "internal": 8300,
      "external": 8300,
      "protocol": "tcp"
    },
    {
      "internal": 8301,
      "external": 8301,
      "protocol": "tcp"
    }
  ]
}

JSON to HCL conversion

/yj-linux-amd64 -jc < testing.tfvars.json
"availability_zone_names" = ["us-east-1a", "us-west-1c"]

"cluster_min_nodes" = 2

"docker_ports" = {
  "external" = 8300

  "internal" = 8300

  "protocol" = "tcp"
}

"docker_ports" = {
  "external" = 8301

  "internal" = 8301

  "protocol" = "tcp"
}

"image_id" = "ami-123"

Thanks a lot !

@nikolay
Copy link

nikolay commented Sep 26, 2023

JSON to HCL is, indeed, broken.

{
  "map": {
    "a": "1",
    "b": "2",
    "c": "3"
  }
}

gets converted to

"map/a" = "1"

"map/b" = "2"

"map/c" = "3"

which is wrong.

@sclevine
Copy link
Owner

sclevine commented Sep 26, 2023

@nikolay I can't seem to replicate your issue:

stephen@laptop test % yj -jc
{
  "map": {
    "a": "1",
    "b": "2",
    "c": "3"
  }
}
"map" = {
  "a" = "1"

  "b" = "2"

  "c" = "3"
}

@nj2208 the output you provided above is expected. This is how HCL1 represents multiple list items by default. There is no way to recover the original version, since that information is lost when the file is converted to JSON.

@nikolay
Copy link

nikolay commented Sep 27, 2023

@sclevine It was my fault, sorry! 🤦🏻 All is good, but it would be nice if the unnecessary quotes were not added so that HCL looks more natural. In general, any valid JSON file is a valid HCL, so there is no need to do anything, but the value of this tool is to generate something more useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants