hcl icon indicating copy to clipboard operation
hcl copied to clipboard

how to convert json to hcl using hclv2?

Open yaohy2014 opened this issue 1 year ago • 4 comments

yaohy2014 avatar Oct 10 '23 11:10 yaohy2014

Hi @yaohy2014 !

You can use the Parse function.

nikpivkin avatar Oct 10 '23 13:10 nikpivkin

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 ?

yaohy2014 avatar Oct 10 '23 13:10 yaohy2014

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.

apparentlymart avatar Oct 10 '23 15:10 apparentlymart

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.

genelet avatar Nov 16 '23 11:11 genelet