├── .gitignore ├── go.mod ├── Makefile ├── README.md └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | /jsonify 2 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/fgeller/jsonify 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | export SHELL:=/usr/bin/env bash -O extglob -c 2 | export GO15VENDOREXPERIMENT:=1 3 | 4 | build: GOOS ?= darwin 5 | build: GOARCH ?= amd64 6 | build: 7 | rm -f jsonify 8 | GOOS=${GOOS} GOARCH=${GOARCH} go build . 9 | 10 | release-linux: 11 | GOOS=linux $(MAKE) build 12 | file jsonify 13 | tar Jcf jsonify-`git describe --abbrev=0 --tags`-linux-amd64.txz jsonify 14 | 15 | release-darwin: 16 | GOOS=darwin $(MAKE) build 17 | file jsonify 18 | tar Jcf jsonify-`git describe --abbrev=0 --tags`-darwin-amd64.txz jsonify 19 | 20 | release: clean release-linux release-darwin 21 | 22 | clean: 23 | rm -f jsonify 24 | rm -f jsonify-*.txz 25 | 26 | run: build 27 | ./jsonify 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jsonify 2 | 3 | Some reasons why you might be interested: 4 | 5 | * Quickly produce JSON output based on command line arguments 6 | * Simple syntax to interpret values as strings or arbitrary JSON values 7 | * Supports reading file contents for easy escaping 8 | 9 | ## Installation 10 | 11 | * Downloads are available from the [Releases](https://github.com/fgeller/jsonify/releases) section. 12 | * `go get github.com/fgeller/jsonify && go install github.com/fgeller/jsonify` 13 | * Via homebrew on OSX: `brew tap fgeller/tap && brew install jsonify` 14 | 15 | ## Usage 16 | 17 | jsonify [[-|=]name value]... 18 | 19 | Converts arguments into JSON output. 20 | 21 | ## Details 22 | 23 | * `-name` causes the value to be interpreted as a string. 24 | * `=name` causes the value to be interpreted as a JSON value. 25 | * If the value is a valid file path, it's contents are used as the value. 26 | 27 | ## Examples 28 | 29 | $ # basic value types, ie - vs = 30 | $ jsonify -name hans =age 23 =subscribed true =address null | jq 31 | { 32 | "address": null, 33 | "age": 23, 34 | "name": "hans", 35 | "subscribed": true 36 | } 37 | 38 | $ # nested objects via command substitution 39 | $ jsonify =a `jsonify -name hans` =b `jsonify -name peter` | tee outfile | jq 40 | { 41 | "a": { 42 | "name": "hans" 43 | }, 44 | "b": { 45 | "name": "peter" 46 | } 47 | } 48 | 49 | $ # subshell output as a value to get current date 50 | $ # reading contents of "outfile" from previous invocation 51 | $ jsonify -date "$(date)" =content outfile | jq 52 | { 53 | "content": { 54 | "a": { 55 | "name": "hans" 56 | }, 57 | "b": { 58 | "name": "peter" 59 | } 60 | }, 61 | "date": "Thu Mar 17 19:10:04 NZDT 2016" 62 | } 63 | 64 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "os" 7 | ) 8 | 9 | func printUsage() { 10 | fmt.Fprintf(os.Stderr, ` 11 | Usage: 12 | 13 | jsonify [[-|=]name value]... 14 | 15 | Converts arguments into JSON output. 16 | 17 | Details: 18 | 19 | -name causes the value to be interpreted as a string. 20 | =name causes the value to be interpreted as a JSON value. 21 | 22 | If the value is a valid file path, it's contents are used as the value. 23 | 24 | Examples: 25 | 26 | $ jsonify -first_name hans -last_name schmitt | jq 27 | { 28 | "first_name": "hans", 29 | "last_name": "schmitt" 30 | } 31 | 32 | $ jsonify =a `+"`"+`jsonify -name hans`+"`"+` =b `+"`"+`jsonify -name peter`+"`"+` | tee out | jq 33 | { 34 | "a": { 35 | "name": "hans" 36 | }, 37 | "b": { 38 | "name": "peter" 39 | } 40 | } 41 | 42 | $ jsonify -date "$(date)" =content out | jq 43 | { 44 | "content": { 45 | "a": { 46 | "name": "hans" 47 | }, 48 | "b": { 49 | "name": "peter" 50 | } 51 | }, 52 | "date": "Thu Mar 17 19:10:04 NZDT 2016" 53 | } 54 | 55 | More info: 56 | 57 | https://github.com/fgeller/jsonify 58 | 59 | `) 60 | } 61 | 62 | func main() { 63 | 64 | if len(os.Args) == 1 { 65 | printUsage() 66 | os.Exit(1) 67 | } 68 | 69 | if len(os.Args)%2 == 0 { 70 | fmt.Fprintf(os.Stderr, "Expecting even number of arguments.") 71 | printUsage() 72 | os.Exit(1) 73 | } 74 | 75 | var data map[string]interface{} = map[string]interface{}{} 76 | for idx := 1; idx < len(os.Args); idx += 2 { 77 | name := os.Args[idx][1:] 78 | value := os.Args[idx+1] 79 | _, err := os.Stat(value) 80 | if !os.IsNotExist(err) { 81 | bs, err := os.ReadFile(value) 82 | if err != nil { 83 | fmt.Fprintf(os.Stderr, "Could not read contents of file %s. err=%s", value, err) 84 | os.Exit(1) 85 | } 86 | value = string(bs) 87 | } 88 | 89 | if os.Args[idx][0:1] == "=" { 90 | var o interface{} 91 | err = json.Unmarshal([]byte(value), &o) 92 | data[name] = o 93 | } else { 94 | data[name] = value 95 | } 96 | } 97 | 98 | bs, err := json.Marshal(data) 99 | if err != nil { 100 | fmt.Fprintf(os.Stderr, "Failed to marshal to json. err=%s\n", err) 101 | os.Exit(1) 102 | } 103 | 104 | fmt.Printf("%s\n", bs) 105 | } 106 | --------------------------------------------------------------------------------