json package

The encoding/json package implements JSON encoding and decoding functions following the RFC 7159 (opens in a new tab) standard.

official docs (opens in a new tab)

Examples of Marshaling Go struct to JSON

The following code snippet demonstrates converting a list of structs to JSON format:

package main
 
import (
	"encoding/json"
	"fmt"
	"log"
)
 
func main() {
	type Car struct {
		Name, Model, Color string
		WeightInKg         int
	}
 
	cars := []Car{{
		Name:       "Toyota",
		Model:      "Corolla",
		Color:      "Grey",
		WeightInKg: 1160,
	}, {
		Name:       "Kia",
		Model:      "C'eed",
		Color:      "Blue",
		WeightInKg: 1425,
	}, {
		Name:       "Opel",
		Model:      "Astra",
		Color:      "Beige",
		WeightInKg: 1180,
	}}
	bytes, err := json.Marshal(cars)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(bytes))
}

After formatting, the console output will look like this:

[
   {
      "Name":"Toyota",
      "Model":"Corolla",
      "Color":"Grey",
      "WeightInKg":1160
   },
   {
      "Name":"Kia",
      "Model":"C'eed",
      "Color":"Blue",
      "WeightInKg":1425
   },
   {
      "Name":"Opel",
      "Model":"Astra",
      "Color":"Beige",
      "WeightInKg":1180
   }
]

You can run the code snippet in your browser at this link (opens in a new tab).

To read JSON into a struct value in Go, you can use the Unmarshal function. It's important to pass the target value as a reference because any changes made to the value within the method must also be reflected outside the function.

Here's the code in English with the Markdown format:

package main
 
import (
	"encoding/json"
	"fmt"
	"log"
)
 
func main() {
	var jsonBlob = []byte(`[
	{"Name": "John", "Age": 18},
	{"Name": "James", "Age": 32},
	{"Name": "David", "Age": 26}
]`)
	type User struct {
		Name string
		Age  int
	}
	var users []User
	err := json.Unmarshal(jsonBlob, &users)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v", users)
}

You can run it in a browser here (opens in a new tab).