In order to better understand terraform I'd like to be able to translate HCL into the equivalent JSON. I got the HCL parser (https://github.com/hashicorp/hcl) to build and the tests to run but I don't see any command to take in an HCL file and output JSON.
There's a python implementation of HCL and it includes a utility that converts HCL to JSON, but it has some rather strange/unexpected behavior, and I would like to confirm that the behavior comes from the HCL language and isn't specific to the python implementation. e.g.
> cat foo.tf
service {
key = "aaa"
}
service {
key = 0x10
foo = "bar"
}
> hcltool foo.tf
{
"service": [
{
"foo": "bar",
"key": "aaa"
},
{
"key": 16
}
]
}
There is an open source project and you can use for help, but terraform hcl is not standard as they designed. Explain here:
https://github.com/kvz/json2hcl/issues/4#issuecomment-275513256
Sadly, this behavior is caused by Hasicorp's official HCL library due to ambiguities in the HCL format. Therefore, we are not really able to improve this situation. We had similar discussions in the past, if you may want to read more on this topic and why this will probably not be changed in the future:
Related repo that you can install the tool:
https://github.com/kvz/json2hcl
Here's an example fixtures/infra.tf.json being converted to HCL:
$ json2hcl < fixtures/infra.tf.json > fixtures/infra.tf
As a bonus, the conversation the other way around is also supported via the -reverse flag:
$ json2hcl -reverse < fixtures/infra.tf
This site is useful for one-off conversions between HCL, JSON, and YAML: https://www.hcl2json.com/