JSON Beautification Gadget for Go Knowledge

Backend http interface tests often require printing json returns, but the default json presentation is compact and not easy to read, and many people are probably accustomed to using online formatting tools.But if you can beautify directly with json code, why open your browser one more time?

Marsh Beautification of Strct and map

There is such a map or structure.

u := map[string]interface{}{
    "id":   1,
    "name": "poloxue",
}

Use GO's built-in package encoding/json.

If converted using json.Marshal, the code is as follows:

uglyBody, err := json.Marshal(u)
if err != nil {
    panic(err)
}
fmt.Printf("%s\n", uglyBody)

Print results:

{"id":1,"name":"poloxue"}

The effect is not friendly, and if it's a complex map, the result will be very confusing.

You can use json.MarchalIndent with the following code:

body, err := json.MarshalIndent(u, "", "\t")
if err != nil {
    panic(err)
}
fmt.Printf("%s\n", body)

t denotes the indentation effect.The final results are as follows:

{
    "id": 1,
    "name": "poloxue"
}

Looking much more comfortable.

How does the string of JSON beautify?

What if it is a JSON string?For example, the result of uglyBody in the example above.

Method One

Use the Indent method in the encoding/json package.

The code is as follows:

var out bytes.Buffer
err = json.Indent(&out, uglyBody, "", "\t")
if err != nil {
    panic(err)
}
fmt.Printf("%s\n", out.String())

The results are as follows:

{
    "id": 1,
    "name": "poloxue"
}

Method 2

Someone on github implements a JSON pretty package.

Address at: https://github.com/tidwall/pr...

It's very simple to use, and the code is as follows:

fmt.Printf("%s\n", pretty.Pretty(uglyBody))

The results are as follows:

{
  "id": 1,
  "name": "poloxue"
}

This library does not rely on encoding/json for code-only string parsing.And you can do color beautification.

Sample code for color beautification:

fmt.Printf("%s\n", pretty.Color(uglyBody, pretty.TerminalStyle))
fmt.Printf("%s\n", pretty.Color(pretty.Pretty(uglyBody), pretty.TerminalStyle))

pretty.TerminalStyle represents the coloring style of the print console as follows:

SO beautiful!

A JSON Beautification Gadget

Using these described above, develop a very simple gadget with the following code:

package main

import (
    "flag"
    "fmt"

    "github.com/tidwall/pretty"
)

var help bool
var uglyJson string
var indent string

func init() {
    flag.BoolVar(&help, "h", false, "show help")
    flag.StringVar(&uglyJson, "s", "", "json string")
    flag.StringVar(&indent, "i", "\t", "indent string, default tab")
}

func main() {
    flag.Parse()

    if help {
        fmt.Printf("usage:\n\n")
        flag.PrintDefaults()
        return
    }

    var Options = &pretty.Options{Width: 80, Prefix: "", Indent: indent, SortKeys: false}
    fmt.Printf("%s\n", pretty.Color(pretty.PrettyOptions([]byte(uglyJson), Options), pretty.TerminalStyle))
}

Once the compilation is successful, it is ready for use.For example, the generated executable is named prettyjson, which beautifies

{"id":1,"name":"poloxue"}. 

The following commands are executed:

$ ./jsonpretty -s '{"id":1,"name":"poloxue"}'

The results are as follows:

Some thoughts

For scenarios with very large JSON data, this is easy to read.If you want to find a field quickly, you can do it through grep, but in this case you can't see the context.One solution you came up with was to achieve contextual presentation through the grep-A-B-C option.Another way to do this is if the command supports specifying the maximum level of formatting, but it needs to be redeveloped.

Tags: Go JSON encoding github

Posted on Sat, 31 Aug 2019 05:16:09 -0700 by ady01