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

how to convert json to hcl using hclv2? #632

Open
yaohy2014 opened this issue Oct 10, 2023 · 4 comments
Open

how to convert json to hcl using hclv2? #632

yaohy2014 opened this issue Oct 10, 2023 · 4 comments

Comments

@yaohy2014
Copy link

No description provided.

@nikpivkin
Copy link

Hi @yaohy2014 !

You can use the Parse function.

@yaohy2014
Copy link
Author

Hi @yaohy2014 !

You can use the Parse function.

this function returned value is *hcl.File, I want the returned value is string. So is there any func to convert *hcl.File to string ?

@apparentlymart
Copy link
Member

Hi @yaohy2014,

HCL is a schema-driven system and the JSON variant relies on schema to resolve ambiguity caused by the limited JSON infoset.

Therefore any tool that converts between the two syntaxes must be application-specific so it can know about the schema of the language being converted. HCL is just a toolkit for building languages, not a language itself.

With that said, it's not typically necessary to convert between the two because applications that accept HCL should typically accept both syntaxes directly themselves, and so you can just pass the JSON variant directly to the application rather than converting it first as a separate step. That is why most existing HCL-based languages don't offer an external conversion mechanism: instead, they use the syntax-agnostic HCL API to abstract away the differences between the two at decoding time.

@genelet
Copy link

genelet commented Nov 16, 2023

Just posted this link in another Issue: https://medium.com/@peterbi_91340/marshal-and-unmarshal-hcl-files-1-3-d7591259a8d6

The package mentioned has also a CLI tool to convert between HCL and JSON.

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

No branches or pull requests

4 participants