This repository demonstrates how to omit empty fields from JSON output in Go using the omitempty
struct tag. It showcases how to use Go's encoding/json
package to exclude fields that have zero values during serialization.
- This example covers how to use the `omitempty` tag to exclude empty or zero-value fields from JSON output.
- It demonstrates defining a struct where certain fields are omitted from the JSON output if they have default values (such as an empty string or zero).
package main
import (
"encoding/json"
"fmt"
)
type Person6 struct {
Name string `json:"name"`
Age int `json:"age,omitempty"`
Country string `json:"country,omitempty"`
}
func main() {
// This example shows how to omit empty fields from the JSON output using the omitempty struct tag
person := Person6{
Name: "John Doe",
}
jsonData, err := json.Marshal(person)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}
fmt.Println(string(jsonData))
}
-
Make sure you have Go installed. If not, you can download it from here.
-
Clone this repository:
git clone https://github.com/Rapter1990/go_sample_examples.git
-
Navigate to the
006_omitting_empty_fields
directory:cd go_sample_examples/030_json/006_omitting_empty_fields
-
Run the Go program:
go run 006_omitting_empty_fields.go
When you run the program, you should see the following output:
{"name":"John Doe"}