We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Marshal JSON

If there is a way to unmarshal JSON data, there must be a way to marshal it as well. The json.Marshal function converts a Go struct into a slice of bytes representing JSON data.

Example

type Board struct {
	Id       int    `json:"id"`
	Name     string `json:"name"`
	TeamId   int    `json:"team"`
	TeamName string `json:"team_name"`
}

board := Board{
	Id:       1,
	Name:     "API",
	TeamId:   9001,
	TeamName: "Backend",
}

data, err := json.Marshal(board)
if err != nil {
	log.Fatal(err)
}
fmt.Println(string(data))
// {"id":1,"name":"API","team":9001,"team_name":"Backend"}

Assignment

Complete the marshalAll function. It accepts a slice of "items", which can be of any type. The expectation is that they are structs of various forms. It should return a slice of slices of bytes (I didn't stutter) [][]byte.