如何将HashiCorp配置语言转换为JSON?

huangapple go评论111阅读模式
英文:

How do I convert HashiCorp Configuration Language into JSON?

问题

为了更好地理解 Terraform,我想要能够将 HCL 转换为等效的 JSON。我已经成功构建了 HCL 解析器(https://github.com/hashicorp/hcl)并运行了测试,但我没有找到任何命令可以接受一个 HCL 文件并输出 JSON。

有一个 Python 实现的 HCL,它包含一个将 HCL 转换为 JSON 的实用工具,但它有一些相当奇怪/意外的行为,我想确认这种行为是来自 HCL 语言而不是特定于 Python 实现。例如:

> cat foo.tf 
service {
    key = "aaa"
}

service {
    key = 0x10
    foo = "bar"
}

> hcltool foo.tf 
{
    "service": [
        {
            "foo": "bar",
            "key": "aaa"
        },
        {
            "key": 16
        }
    ]
}
英文:

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
        }
    ]
}

答案1

得分: 12

有一个开源项目叫做json2hcl,你可以使用它来帮助你,但是 Terraform HCL 不是按照他们设计的标准来的。在这里解释了这个问题:

https://github.com/kvz/json2hcl/issues/4#issuecomment-275513256

不幸的是,这个行为是由 Hasicorp 官方的 HCL 库引起的,因为 HCL 格式存在歧义。因此,我们无法真正改善这种情况。我们在过去也进行过类似的讨论,如果你想要了解更多关于这个主题以及为什么这可能在未来不会改变的信息:

你可以安装这个相关的仓库来使用这个工具:

https://github.com/kvz/json2hcl

这里有一个将 fixtures/infra.tf.json 转换为 HCL 的示例:

$ json2hcl < fixtures/infra.tf.json > fixtures/infra.tf

作为额外的功能,也支持通过 -reverse 标志来进行反向转换:

$ json2hcl -reverse < fixtures/infra.tf
英文:

There is an open source project json2hcl 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 &lt; fixtures/infra.tf.json &gt; fixtures/infra.tf

As a bonus, the other way around is also supported via the -reverse flag:

$ json2hcl -reverse &lt; fixtures/infra.tf

答案2

得分: 5

这个网站对于在HCL、JSON和YAML之间进行一次性转换非常有用:
https://www.hcl2json.com/

英文:

This site is useful for one-off conversions between HCL, JSON, and YAML:
https://www.hcl2json.com/

答案3

得分: 1

你也可以使用HCL2,如下所示:

with open('main.tf', 'r') as file:
    temp_dict = hcl2.load(file)
英文:

You can also use HCL2, like below:

with open(&#39;main.tf&#39;, &#39;r&#39;) as file:
    temp_dict = hcl2.load(file)

huangapple
  • 本文由 发表于 2017年8月4日 12:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/45498094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定