Go Language Processing JSON-Generating JSON String by Marshal

Keywords: JSON Ruby encoding Go

Using the encodong/json standard library built in Go language, we can easily generate and analyze data in JSON format.

func Marshal(v interface{}) ([]byte, error)

From the return value, we can see that the function has two return values, one is the json code of the incoming parameter v, the type is []byte, and the other is error.
Official documents provide an example:

package main

import (
	"fmt"
	"encoding/json"
)

type ColorGroup struct {
	ID     int
	Name   string
	Colors []string
}

func main() {
	group := ColorGroup {
	    ID:     1,
	    Name:   "Reds",
	    Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
	}

	b, err := json.Marshal(group)
	if err != nil {
	    fmt.Println("error:", err)
	}
	
	fmt.Println(string(b))
}

Output:

{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}

As you can see, the variable names in the structure we define are capitalized. What's the matter? It turns out that only uppercase variables can be derived.
Slightly alter the code above, turn name into lowercase, and see what happens.

package main

import (
	"fmt"
	"encoding/json"
)

type ColorGroup struct {
	ID     int
	name   string  //Here the name is lowercase
	Colors []string
}

func main() {
	group := ColorGroup {
	    ID:     1,
	    name:   "Reds", //Here the name is lowercase
	    Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
	}

	b, err := json.Marshal(group)
	if err != nil {
	    fmt.Println("error:", err)
	}
	
	fmt.Println(string(b))
}

Output:

{"ID":1,"Colors":["Crimson","Red","Ruby","Maroon"]}

In order to lower-case the output json string, what should be done? go also provides a way.
To label a field:
Give an example:

type ColorGroup struct {
	ID     int
	Name   string  `json:"name"`
	Colors []string
}

We added a field label after Name, noting that it was wrapped in reverse quotation marks, the key above the Tab key on the keyboard.
This field label means that when you export, use the name name.

Output results:

{"ID":1,"name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}

This field label has other uses, such as:

type ColorGroup struct {
	ID     int
	Name   string  `json:"-"`
	Colors []string
}

At this time, the output is as follows:

{"ID":1,"Colors":["Crimson","Red","Ruby","Maroon"]}

// Field ignored by this package
Field int json:"-"
// The key of the field in json is "myName"
Field int json:"myName"

Detailed usage can be referred to go official documents, not listed here.
https://studygolang.com/pkgdoc
View encoding/json

Finally, let's look at a concrete example:

{
    "first fruit":
    {
        "describe":"an apple",
        "icon":"appleIcon",
        "name":"apple"
    },
    "second fruit":
    {
        "describe":"an orange",
        "icon":"orangeIcon",
        "name":"orange"
    },
    "three fruit array":
    [
        "eat 0",
        "eat 1",
        "eat 2",
        "eat 3",
        "eat 4"
    ]
}

For example, if we want to generate such a json string, how can go language write it?

Posted by newguy45 on Mon, 30 Sep 2019 08:24:51 -0700