How to work with JSON data in Go?

To work with JSON data in Go, you can use the built-in package encoding/json. Here's a step-by-step guide on how to work with JSON data in Go:

  1. Parse JSON data into Go types: To parse JSON data into Go types, you need to define a struct that represents the structure of the JSON data. Each field in the struct should be tagged with the corresponding JSON property name. Then, you can use the json.Unmarshal() function to parse the JSON data into the Go struct.

    Here's an example:

    type Person struct { Name string `json:"name"` Age int `json:"age"` Email string `json:"email"` } jsonString := `{"name":"John Doe","age":30,"email":"[email protected]"}` var person Person err := json.Unmarshal([]byte(jsonString), &person) if err != nil { fmt.Println("Error:", err) } else { fmt.Println("Name:", person.Name) fmt.Println("Age:", person.Age) fmt.Println("Email:", person.Email) }

    Output:

    Name: John Doe Age: 30 Email: [email protected]
  2. Convert Go types to JSON data: To convert Go types to JSON data, you can use the json.Marshal() function. It returns a byte slice containing the JSON representation of the Go value.

    Here's an example:

    person := Person{ Name: "John Doe", Age: 30, Email: "[email protected]", } jsonBytes, err := json.Marshal(person) if err != nil { fmt.Println("Error:", err) } else { jsonString := string(jsonBytes) fmt.Println(jsonString) }

    Output:

    {"name":"John Doe","age":30,"email":"[email protected]"}
  3. Decode and encode JSON from files or network streams: To work with JSON data from files or network streams, you can use the json.Decoder and json.Encoder types. They provide methods like Decode() and Encode() for reading and writing JSON data respectively.

    Here's an example of reading JSON data from a file:

    type Person struct { Name string `json:"name"` Age int `json:"age"` Email string `json:"email"` } file, err := os.Open("data.json") if err != nil { fmt.Println("Error:", err) } defer file.Close() decoder := json.NewDecoder(file) var person Person err = decoder.Decode(&person) if err != nil { fmt.Println("Error:", err) } else { fmt.Println("Name:", person.Name) fmt.Println("Age:", person.Age) fmt.Println("Email:", person.Email) }

    Here's an example of writing JSON data to a file:

    type Person struct { Name string `json:"name"` Age int `json:"age"` Email string `json:"email"` } file, err := os.Create("data.json") if err != nil { fmt.Println("Error:", err) } defer file.Close() encoder := json.NewEncoder(file) person := Person{ Name: "John Doe", Age: 30, Email: "[email protected]", } err = encoder.Encode(person) if err != nil { fmt.Println("Error:", err) }

This is a basic overview of working with JSON data in Go using the encoding/json package. There are more advanced features available in the package, such as handling custom data types, omitting fields, and converting JSON to maps or slices. The package documentation is a great resource to explore these features further.