bgp bgp - 10 months ago 38
JSON Question

How can I pretty-print JSON using Go?

Does anyone know of a simple way to pretty-print JSON output in Go?

The stock package does not seem to include functionality for this (EDIT: it does, see accepted answer) and a quick google doesn't turn up anything obvious.

Uses I'm looking for are both pretty-printing the result of

and just formatting a string full of JSON from wherever, so it's easier to read for debug purposes.


By pretty-print, I assume you mean indented, like so

    "data": 1234

rather than


The easiest way to do this is with MarshalIndent, which will let you specify how you would like it indented via the indent argument. Thus, json.MarshalIndent(data, "", " ") will pretty-print using four spaces for indentation.