Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tmccombs/hcl2json
Convert hcl2 to json
https://github.com/tmccombs/hcl2json
Last synced: about 23 hours ago
JSON representation
Convert hcl2 to json
- Host: GitHub
- URL: https://github.com/tmccombs/hcl2json
- Owner: tmccombs
- License: apache-2.0
- Created: 2019-07-22T06:43:31.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-01-27T07:52:27.000Z (25 days ago)
- Last Synced: 2025-02-13T14:07:08.270Z (8 days ago)
- Language: Go
- Size: 178 KB
- Stars: 402
- Watchers: 8
- Forks: 53
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - tmccombs/hcl2json - Convert hcl2 to json (Go)
- trackawesomelist - hcl2json (⭐396) - Convert hcl2 to json. (Recently Updated / [Feb 10, 2025](/content/2025/02/10/README.md))
README
# hcl2json
This is a tool to convert from [HCL](https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md) to json, to make it
easier for non-go applications and scripts to process HCL inputs (such as terraform config).If passed the `-pack` option, it converts to the JSON pack format used by
[hclpack](https://godoc.org/github.com/hashicorp/hcl2/hclpack), which contains the original structure of the document. However,
all it has for expressions is the source text, which isn't always very useful.If no options are passed, it converts the native HCL file to an (almost) equivalent HCL JSON file. Note, however, that there are
some corner cases where it may not be exactly equivalent, especially if the target application makes use of [static
analysis](https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#static-analysis).## Alternatives
At the time this project created, there weren't really any other object. However, the HCL project itself now includes an
[`hcldec`](https://github.com/hashicorp/hcl/tree/main/cmd/hcldec) command that can convert hcl to json if you give it a
specification. One advantage of hcl2json is it doesn't require a specification, so for use cases where the heuristics are correct
it may be easier to use. However, if you have cases hit the limitations of conversion listed above, `hcldec` is probably a
better fit.## Usage
```sh
# convert a file from hcl to json
$ hcl2json some-file.hcl > out.json
# reading from stdin also works
$ hcl2json < infile.hcl > out.json
# simplify any expressions that don't use variables or unknown functions
$ hcl2json -simplify infile.hcl > out.json
```If you use the docker image, you can invoke with
```sh
$ docker run -it --rm -v $PWD:/tmp tmccombs/hcl2json /tmp/infile.hcl
```## Installation
### Mac OS
Install with [Homebrew](https://formulae.brew.sh/formula/hcl2json):
```shell
brew install hcl2json
````hcl2json` can also be installed via [MacPorts](https://ports.macports.org/port/hcl2json/):
```shell
sudo port install hcl2json
```### Others
Prebuilt binaries are available on the [releases page](https://github.com/tmccombs/hcl2json/releases). There is also a docker image on [dockerhub](https://hub.docker.com/r/tmccombs/hcl2json).
Alternatively you can build from source (see next section).
## Building
You can build and install `hcl2json` using `go get`. Since `hcl2json` uses Go modules, you will need to run this as
`GO11MODULE=on go get github.com/tmccombs/hcl2json`.Alternatively, you can clone and build the repository:
```
$ git clone https://github.com/tmccombs/hcl2json
$ cd hcl2json
$ go build
```This will build an `hcl2json` executable in the directory.