Last active
December 13, 2022 18:37
-
-
Save pydevops/0402032d38a24c2389115124161d2715 to your computer and use it in GitHub Desktop.
pretty print json yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Please note we need to single quote the json string