pretty print JSON in golang

May 22, 2016    general blog

JSON is a very common standard to transmit data objects. go provides great support for JSON. JSON package in standard library provides the methods to work with JSON in a go program. Generally we use JSON to tranmit the data but sometime we need to print the JSON data too. If a human eye is going to look at that data, it is a good idea to pretty print that JSON. Here is small function that will do that:

func prettyPrintJSON(b []byte) ([]byte, error) {
	var out bytes.Buffer
	err := JSON.Indent(&out, b, "", "    ")
	return out.Bytes(), err
}

This function takes a byte array and indent the JSON. Each element in JSON object begins on a new line.