JSON Encode - Pretty Print
·
250 words
·
2 minutes read
Is JSON for computers or humans to read? Well, hopefully with this snippet it can be for both. Instead of just dumping all the json in one line, we instead format it and indent it for readability purposes.
Using the MarshalIndent
function in the json
package we’re able to not only specify the data to encode, but also a prefix and an indentation string. In our example we’re not to worried about the prefix, but the indentation (3rd parameter) allows us to structure our code. We’re using 4 spaces for each indent level, but you can pick what ever suits you.
|
|