Sunday, June 16, 2024
HomeGolang3 methods to fairly print JSON in Go (Golang)

3 methods to fairly print JSON in Go (Golang)



In on a regular basis programming work (particularly as an internet developer), you usually need to debug and analyze JSON responses. In such circumstances, it’s good to show them with an acceptable indentation for higher readability, which is usually referred to as fairly printing or beautifying JSON. In Go, we are able to do that in a number of methods utilizing the encoding/json bundle.

JSON fairly print by marshaling worth

Operate json.MarshalIndent generates JSON encoding of the worth with indentation. You’ll be able to specify a prefix of every JSON line and indent copied a number of occasions in line with the indentation stage. In our instance, we pretty-print JSON utilizing 4 areas for indentation.

bundle predominant

import (
    "encoding/json"
    "fmt"
    "log"
)

func PrettyStruct(information interface{}) (string, error) {
    val, err := json.MarshalIndent(information, "", "    ")
    if err != nil {
        return "", err
    }
    return string(val), nil
}

kind Fruit struct {
    Identify  string `json:"title"`
    Coloration string `json:"colour"`
}

func predominant() {
    fruit := Fruit{
        Identify:  "Strawberry",
        Coloration: "crimson",
    }
    res, err := PrettyStruct(fruit)
    if err != nil {
        log.Deadly(err)
    }
    fmt.Println(res)
}

JSON fairly print by encoding worth

For those who use json.Encode, you’ll be able to set indentation by way of Encoder.SetIndent technique equally to as in marshaling, by defining a prefix and indent.

bundle predominant

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "log"
)

func PrettyEncode(information interface{}, out io.Author) error {
    enc := json.NewEncoder(out)
    enc.SetIndent("", "    ")
    if err := enc.Encode(information); err != nil {
        return err
    }
    return nil
}

kind Fruit struct {
    Identify  string `json:"title"`
    Coloration string `json:"colour"`
}

func predominant() {
    fruit := Fruit{
        Identify:  "Strawberry",
        Coloration: "crimson",
    }
    var buffer bytes.Buffer
    err := PrettyEncode(fruit, &buffer)
    if err != nil {
        log.Deadly(err)
    }
    fmt.Println(buffer.String())
}

Fairly print JSON string

Package deal encoding/json additionally has a helpful operate json.Indent to beautify JSON string with out indentation to JSON with indentation. The operate wants the supply JSON, output buffer, prefix, and indent.

bundle predominant

import (
    "bytes"
    "encoding/json"
    "fmt"
    "log"
)

func PrettyString(str string) (string, error) {
    var prettyJSON bytes.Buffer
    if err := json.Indent(&prettyJSON, []byte(str), "", "    "); err != nil {
        return "", err
    }
    return prettyJSON.String(), nil
}

func predominant() {
    fruitJSON := `{"title": "Strawberry", "colour": "crimson"}`
    res, err := PrettyString(fruitJSON)
    if err != nil {
        log.Deadly(err)
    }
    fmt.Println(res)
}

All strategies print JSON string with indentation:

{
    "title": "Strawberry",
    "colour": "crimson"
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments