HiveBrain v1.2.0
Get Started
← Back to all entries
snippetgoCritical

How can I pretty-print JSON using Go?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howprettyusingprintcanjson

Problem

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

I'd like to pretty-print the result of json.Marshal, as well as formatting an existing string of JSON so it's easier to read.

Solution

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.

Code Snippets

{
    "data": 1234
}

Context

Stack Overflow Q#19038598, score: 533

Revisions (0)

No revisions yet.