├── .github ├── dependabot.yml └── workflows │ └── ci.yml ├── .gitignore ├── .goreleaser.yml ├── LICENSE ├── README.md ├── fixtures ├── infra.tf └── infra.tf.json ├── go.mod ├── go.sum └── main.go /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # Keep GitHub Actions up to date with GitHub's Dependabot... 2 | # https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot 3 | # https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem 4 | version: 2 5 | updates: 6 | - package-ecosystem: github-actions 7 | directory: / 8 | groups: 9 | github-actions: 10 | patterns: 11 | - "*" # Group all Actions updates into a single larger pull request 12 | schedule: 13 | interval: weekly 14 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | tags: 8 | - '*' 9 | pull_request: 10 | 11 | jobs: 12 | build: 13 | 14 | runs-on: ubuntu-latest 15 | 16 | strategy: 17 | matrix: 18 | go-version: [1.22.5] 19 | 20 | steps: 21 | - uses: actions/checkout@v4 22 | - uses: actions/setup-go@v5 23 | with: 24 | go-version: ${{ matrix.go-version }} 25 | - uses: goreleaser/goreleaser-action@v6 26 | with: 27 | args: ${{ github.ref_type == 'tag' && 'release' || 'release --snapshot' }} 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 30 | - uses: actions/upload-artifact@v4 31 | with: 32 | name: json2hcl 33 | path: dist/* 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | 26 | infra.tf 27 | 28 | env.sh 29 | -------------------------------------------------------------------------------- /.goreleaser.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | builds: 4 | - 5 | goos: 6 | - windows 7 | - darwin 8 | - linux 9 | goarch: 10 | - amd64 11 | - arm64 12 | 13 | # .goreleaser.yaml 14 | release: 15 | # Repo in which the release will be created. 16 | # Default is extracted from the origin remote URL or empty if it is privately hosted. 17 | github: 18 | owner: kvz 19 | name: json2hcl 20 | 21 | # What to do with the release notes in case there the release already exists. 22 | # 23 | # Valid options are: 24 | # - `keep-existing`: keep the existing notes 25 | # - `append`: append the current release notes to the existing notes 26 | # - `prepend`: prepend the current release notes to the existing notes 27 | # - `replace`: replace existing notes 28 | # 29 | # Default is `keep-existing`. 30 | mode: replace 31 | 32 | # Header template for the release body. 33 | # Defaults to empty. 34 | header: | 35 | ## Welcome to the automatically created release at ({{ .Date }}) 36 | 37 | Welcome to this new release! 38 | 39 | # Footer template for the release body. 40 | # Defaults to empty. 41 | footer: | 42 | ## Thanks! 43 | 44 | Those were the changes on {{ .Tag }}! 45 | 46 | # You can change the name of the release. 47 | # Default is `{{.Tag}}` on OSS and `{{.PrefixedTag}}` on Pro. 48 | name_template: "v{{.Version}}" 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Kevin van Zonneveld 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/kvz/json2hcl.svg?branch=master)](https://travis-ci.org/kvz/json2hcl) 2 | 3 | # json2hcl (and hcl2json) 4 | 5 | Convert JSON to HCL and HCL to JSON via STDIN / STDOUT. 6 | 7 | ## Warning 8 | 9 | We don't use json2hcl anymore ourselves, so we can't invest time into it. However, we're still welcoming PRs. 10 | 11 | ## Install 12 | 13 | Check the [releases](https://github.com/kvz/json2hcl/releases) for the latest version. 14 | Then it's just a matter of downloading the right one for you platform, and making the binary 15 | executable. 16 | 17 | ### Linux 18 | 19 | Here's how it could look for 64 bits Linux, if you wanted `json2hcl` available globally inside 20 | `/usr/local/bin`: 21 | 22 | ```bash 23 | curl -SsL https://github.com/kvz/json2hcl/releases/download/v0.0.6/json2hcl_v0.0.6_linux_amd64 \ 24 | | sudo tee /usr/local/bin/json2hcl > /dev/null && sudo chmod 755 /usr/local/bin/json2hcl && json2hcl -version 25 | ``` 26 | 27 | ### OSX 28 | 29 | Here's how it could look for 64 bits Darwin, if you wanted `json2hcl` available globally inside 30 | `/usr/local/bin`: 31 | 32 | ```bash 33 | curl -SsL https://github.com/kvz/json2hcl/releases/download/v0.0.6/json2hcl_v0.0.6_darwin_amd64 \ 34 | | sudo tee /usr/local/bin/json2hcl > /dev/null && sudo chmod 755 /usr/local/bin/json2hcl && json2hcl -version 35 | ``` 36 | 37 | ## Use 38 | 39 | Here's an example [`fixtures/infra.tf.json`](fixtures/infra.tf.json) being 40 | converted to HCL: 41 | 42 | ```bash 43 | $ json2hcl < fixtures/infra.tf.json 44 | "output" "arn" { 45 | "value" = "${aws_dynamodb_table.basic-dynamodb-table.arn}" 46 | } 47 | ... rest of HCL truncated 48 | ``` 49 | 50 | Typical use would be 51 | 52 | ```bash 53 | $ json2hcl < fixtures/infra.tf.json > fixtures/infra.tf 54 | ``` 55 | 56 | ## hcl2json 57 | 58 | As a bonus, the conversion the other way around is also supported via the `-reverse` flag: 59 | 60 | ```bash 61 | $ json2hcl -reverse < fixtures/infra.tf 62 | { 63 | "output": [ 64 | { 65 | "arn": [ 66 | { 67 | "value": "${aws_dynamodb_table.basic-dynamodb-table.arn}" 68 | } 69 | ] 70 | }, 71 | ... rest of JSON truncated 72 | ] 73 | } 74 | ``` 75 | 76 | ## Development 77 | 78 | ```bash 79 | mkdir -p ~/go/src/github.com/kvz 80 | cd ~/go/src/github.com/kvz 81 | git clone git@github.com:kvz/json2hcl.git 82 | cd json2hcl 83 | go get 84 | ``` 85 | 86 | ## Why? 87 | 88 | If you don't know HCL, read [Why HCL](https://github.com/hashicorp/hcl#why). 89 | 90 | As for why json2hcl and hcl2json, we're building a tool called Frey that marries multiple underlying 91 | tools. We'd like configuration previously written in YAML or TOML to now be in HCL now as well. 92 | It's easy enough to convert the mentioned formats to JSON, and strictly speaking HCL is already 93 | able to read JSON natively, so why the extra step? 94 | 95 | We're doing this for readability and maintainability, we wanted to save 96 | our infra recipes as HCL directly in our repos, instead of only having machine readable intermediate 97 | JSON that we'd need to hack on. This saves time spotting problems, and makes the experience somewhat 98 | enjoyable even. 99 | 100 | In the off-chance you too have machine-readable JSON and are interested in converting that 101 | to the more human-being friendly HCL format, we thought we'd share this. 102 | 103 | It's no rocket science, we're using already available HashiCorp libraries to support the conversion, 104 | HashiCorp could have easily released their own tools around this, and perhaps they will, but 105 | so far, they haven't. 106 | 107 | ## Changelog 108 | 109 | ### Ideabox (Unplanned) 110 | 111 | - [ ] Give the README.md some love 112 | 113 | ### v0.0.7 (Unreleased) 114 | 115 | - [ ] Tests 116 | 117 | ### v0.0.6 (2016-09-06) 118 | 119 | - [x] Deprecate goxc in favor of native builds 120 | 121 | ### v0.0.5 (2016-09-06) 122 | 123 | - [x] Add hcl2json via the `-reverse` flag 124 | 125 | ### v0.0.4 (2016-09-05) 126 | 127 | - [x] Error handling 128 | - [x] Cross-compiling and shipping releases 129 | 130 | ## Contributors 131 | 132 | - [Marius Kleidl](https://github.com/Acconut) 133 | - [Kevin van Zonneveld](https://github.com/kvz) 134 | -------------------------------------------------------------------------------- /fixtures/infra.tf: -------------------------------------------------------------------------------- 1 | "output" = { 2 | "arn" = { 3 | "value" = "${aws_dynamodb_table.basic-dynamodb-table.arn}" 4 | } 5 | } 6 | 7 | "output" = { 8 | "endpoint" = { 9 | "value" = "http://dynamodb.com:8080/endpoint/${aws_dynamodb_table.basic-dynamodb-table.arn}" 10 | } 11 | } 12 | 13 | "provider" = { 14 | "aws" = { 15 | "access_key" = "${var.FREY_AWS_ACCESS_KEY}" 16 | 17 | "region" = "us-east-1" 18 | 19 | "secret_key" = "${var.FREY_AWS_SECRET_KEY}" 20 | } 21 | } 22 | 23 | "resource" = { 24 | "aws_dynamodb_table" = { 25 | "basic-dynamodb-table" = { 26 | "attribute" = { 27 | "name" = "TopScore" 28 | 29 | "type" = "N" 30 | } 31 | 32 | "attribute" = { 33 | "name" = "UserId" 34 | 35 | "type" = "N" 36 | } 37 | 38 | "attribute" = { 39 | "name" = "GameTitle" 40 | 41 | "type" = "S" 42 | } 43 | 44 | "global_secondary_index" = { 45 | "hash_key" = "GameTitle" 46 | 47 | "name" = "GameTitleIndex" 48 | 49 | "non_key_attributes" = ["UserId"] 50 | 51 | "projection_type" = "INCLUDE" 52 | 53 | "range_key" = "TopScore" 54 | 55 | "read_capacity" = 10 56 | 57 | "write_capacity" = 10 58 | } 59 | 60 | "hash_key" = "UserId" 61 | 62 | "name" = "GameScores" 63 | 64 | "range_key" = "GameTitle" 65 | 66 | "read_capacity" = 20 67 | 68 | "write_capacity" = 20 69 | } 70 | } 71 | } 72 | 73 | "variable" = { 74 | "FREY_OPENSTACK_TENANT_NAME" = { 75 | "type" = "string" 76 | } 77 | } 78 | 79 | "variable" = { 80 | "FREY_AWS_ACCESS_KEY" = { 81 | "type" = "string" 82 | } 83 | } 84 | 85 | "variable" = { 86 | "FREY_AWS_SECRET_KEY" = { 87 | "type" = "string" 88 | } 89 | } 90 | 91 | "variable" = { 92 | "FREY_OPENSTACK_PROJECT_NAME" = { 93 | "type" = "string" 94 | } 95 | } 96 | 97 | "variable" = { 98 | "FREY_OPENSTACK_PASSWORD" = { 99 | "type" = "string" 100 | } 101 | } 102 | 103 | "variable" = { 104 | "FREY_OPENSTACK_TENANT_ID" = { 105 | "type" = "string" 106 | } 107 | } 108 | 109 | "variable" = { 110 | "FREY_OPENSTACK_USERNAME" = { 111 | "type" = "string" 112 | } 113 | } 114 | 115 | "variable" = { 116 | "FREY_DO_TOKEN" = { 117 | "type" = "string" 118 | } 119 | } 120 | 121 | "variable" = { 122 | "FREY_ENCRYPTION_SECRET" = { 123 | "type" = "string" 124 | } 125 | } 126 | 127 | "variable" = { 128 | "FREY_OPENSTACK_AUTH_URL" = { 129 | "type" = "string" 130 | } 131 | } 132 | 133 | "variable" = { 134 | "FREY_OPENSTACK_EXTERNAL_GATEWAY" = { 135 | "type" = "string" 136 | } 137 | } -------------------------------------------------------------------------------- /fixtures/infra.tf.json: -------------------------------------------------------------------------------- 1 | { 2 | "output": [ 3 | { 4 | "arn": [ 5 | { 6 | "value": "${aws_dynamodb_table.basic-dynamodb-table.arn}" 7 | } 8 | ] 9 | }, 10 | { 11 | "endpoint": [ 12 | { 13 | "value": "http://dynamodb.com:8080/endpoint/${aws_dynamodb_table.basic-dynamodb-table.arn}" 14 | } 15 | ] 16 | } 17 | ], 18 | "provider": [ 19 | { 20 | "aws": [ 21 | { 22 | "access_key": "${var.FREY_AWS_ACCESS_KEY}", 23 | "region": "us-east-1", 24 | "secret_key": "${var.FREY_AWS_SECRET_KEY}" 25 | } 26 | ] 27 | } 28 | ], 29 | "resource": [ 30 | { 31 | "aws_dynamodb_table": [ 32 | { 33 | "basic-dynamodb-table": [ 34 | { 35 | "attribute": [ 36 | { 37 | "name": "TopScore", 38 | "type": "N" 39 | }, 40 | { 41 | "name": "UserId", 42 | "type": "N" 43 | }, 44 | { 45 | "name": "GameTitle", 46 | "type": "S" 47 | } 48 | ], 49 | "global_secondary_index": [ 50 | { 51 | "hash_key": "GameTitle", 52 | "name": "GameTitleIndex", 53 | "non_key_attributes": [ 54 | "UserId" 55 | ], 56 | "projection_type": "INCLUDE", 57 | "range_key": "TopScore", 58 | "read_capacity": 10, 59 | "write_capacity": 10 60 | } 61 | ], 62 | "hash_key": "UserId", 63 | "name": "GameScores", 64 | "range_key": "GameTitle", 65 | "read_capacity": 20, 66 | "write_capacity": 20 67 | } 68 | ] 69 | } 70 | ] 71 | } 72 | ], 73 | "variable": [ 74 | { 75 | "FREY_OPENSTACK_TENANT_NAME": [ 76 | { 77 | "type": "string" 78 | } 79 | ] 80 | }, 81 | { 82 | "FREY_AWS_ACCESS_KEY": [ 83 | { 84 | "type": "string" 85 | } 86 | ] 87 | }, 88 | { 89 | "FREY_AWS_SECRET_KEY": [ 90 | { 91 | "type": "string" 92 | } 93 | ] 94 | }, 95 | { 96 | "FREY_OPENSTACK_PROJECT_NAME": [ 97 | { 98 | "type": "string" 99 | } 100 | ] 101 | }, 102 | { 103 | "FREY_OPENSTACK_PASSWORD": [ 104 | { 105 | "type": "string" 106 | } 107 | ] 108 | }, 109 | { 110 | "FREY_OPENSTACK_TENANT_ID": [ 111 | { 112 | "type": "string" 113 | } 114 | ] 115 | }, 116 | { 117 | "FREY_OPENSTACK_USERNAME": [ 118 | { 119 | "type": "string" 120 | } 121 | ] 122 | }, 123 | { 124 | "FREY_DO_TOKEN": [ 125 | { 126 | "type": "string" 127 | } 128 | ] 129 | }, 130 | { 131 | "FREY_ENCRYPTION_SECRET": [ 132 | { 133 | "type": "string" 134 | } 135 | ] 136 | }, 137 | { 138 | "FREY_OPENSTACK_AUTH_URL": [ 139 | { 140 | "type": "string" 141 | } 142 | ] 143 | }, 144 | { 145 | "FREY_OPENSTACK_EXTERNAL_GATEWAY": [ 146 | { 147 | "type": "string" 148 | } 149 | ] 150 | } 151 | ] 152 | } 153 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/kvz/json2hcl 2 | 3 | go 1.22.5 4 | 5 | require github.com/hashicorp/hcl v1.0.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= 4 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 5 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "flag" 6 | "fmt" 7 | "io/ioutil" 8 | "os" 9 | 10 | "github.com/hashicorp/hcl" 11 | "github.com/hashicorp/hcl/hcl/printer" 12 | jsonParser "github.com/hashicorp/hcl/json/parser" 13 | ) 14 | 15 | // VERSION is what is returned by the `-v` flag 16 | var Version = "development" 17 | 18 | func main() { 19 | version := flag.Bool("version", false, "Prints current app version") 20 | reverse := flag.Bool("reverse", false, "Input HCL, output JSON") 21 | flag.Parse() 22 | if *version { 23 | fmt.Println(Version) 24 | return 25 | } 26 | 27 | var err error 28 | if *reverse { 29 | err = toJSON() 30 | } else { 31 | err = toHCL() 32 | } 33 | 34 | if err != nil { 35 | fmt.Fprintln(os.Stderr, err) 36 | os.Exit(1) 37 | } 38 | } 39 | 40 | func toJSON() error { 41 | input, err := ioutil.ReadAll(os.Stdin) 42 | if err != nil { 43 | return fmt.Errorf("unable to read from stdin: %s", err) 44 | } 45 | 46 | var v interface{} 47 | err = hcl.Unmarshal(input, &v) 48 | if err != nil { 49 | return fmt.Errorf("unable to parse HCL: %s", err) 50 | } 51 | 52 | json, err := json.MarshalIndent(v, "", " ") 53 | if err != nil { 54 | return fmt.Errorf("unable to marshal json: %s", err) 55 | } 56 | 57 | fmt.Println(string(json)) 58 | 59 | return nil 60 | } 61 | 62 | func toHCL() error { 63 | input, err := ioutil.ReadAll(os.Stdin) 64 | if err != nil { 65 | return fmt.Errorf("unable to read from stdin: %s", err) 66 | } 67 | 68 | ast, err := jsonParser.Parse([]byte(input)) 69 | if err != nil { 70 | return fmt.Errorf("unable to parse JSON: %s", err) 71 | } 72 | 73 | err = printer.Fprint(os.Stdout, ast) 74 | if err != nil { 75 | return fmt.Errorf("unable to print HCL: %s", err) 76 | } 77 | 78 | return nil 79 | } 80 | --------------------------------------------------------------------------------