Skip to content

Instantly share code, notes, and snippets.

@pydevops
Last active December 13, 2022 18:37
Show Gist options
  • Save pydevops/0402032d38a24c2389115124161d2715 to your computer and use it in GitHub Desktop.
Save pydevops/0402032d38a24c2389115124161d2715 to your computer and use it in GitHub Desktop.
pretty print json yaml
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
flatten "github.com/jeremywohl/flatten"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func indent(data []byte) *bytes.Buffer {
buf := new(bytes.Buffer)
json.Indent(buf, data, "", " ")
return buf
}
func flattenBytes(data []byte) (string, error) {
flattened, err := flatten.FlattenString(string(data), "", flatten.DotStyle)
return flattened, err
}
func main() {
data, err := ioutil.ReadAll(os.Stdin)
check(err)
fmt.Println(indent(data))
// fStr, e := flattenBytes(data)
// check(e)
// fmt.Println(fStr)
}
@pydevops
Copy link
Author

Usage

cat json_file | ppj 
echo '{"page": 1, "fruits": ["apple", "peach"]}' | ppj

Please note we need to single quote the json string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment