How can I pretty-print JSON using Go?

MarshalIndent will allow you to output your JSON with indentation and spacing. For example:

{
    "data": 1234
}

The indent argument specifies the series of characters to indent with. Thus, json.MarshalIndent(data, "", " ") will pretty-print using four spaces for indentation.

Leave a Comment