Home [go] json read/write/update method (code example and explanation)
Post
Cancel

[go] json read/write/update method (code example and explanation)

Intro

Introduction

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. In Go, handling JSON data involves reading, writing, and updating JSON content. In this blog post, we will explore how to perform these operations using code examples and explanations.

Reading JSON Data

To read JSON data in Go, we can use the ‘encoding/json’ package. The ‘Unmarshal’ function is used to decode JSON data into a Go structure. Here is an example code snippet to demonstrate how to read JSON data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
	"encoding/json"
	"fmt"
)

type Person struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func main() {
	jsonData := []byte(`{"name": "Alice", "age": 30}`)
	var person Person
	err := json.Unmarshal(jsonData, &person)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("Name:", person.Name)
	fmt.Println("Age:", person.Age)
}

Output:

1
2
Name: Alice
Age: 30

Writing JSON Data

To write JSON data in Go, we can use the ‘encoding/json’ package as well. The ‘Marshal’ function is used to encode a Go structure into JSON data. Here is an example code snippet to demonstrate how to write JSON data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import (
	"encoding/json"
	"fmt"
)

type Person struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func main() {
	person := Person{Name: "Bob", Age: 25}
	jsonData, err := json.Marshal(person)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println(string(jsonData))
}

Output:

1
{"name":"Bob","age":25}

Updating JSON Data

To update JSON data in Go, we can first read the JSON content, make the necessary changes to the corresponding Go structure, and then write the updated content back to a JSON format. Here is an example code snippet to demonstrate how to update JSON data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main

import (
	"encoding/json"
	"fmt"
)

type Person struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func main() {
	jsonData := []byte(`{"name": "Alice", "age": 30}`)
	var person Person
	err := json.Unmarshal(jsonData, &person)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	person.Age = 40
	updatedData, err := json.Marshal(person)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println(string(updatedData))
}

Output:

1
{"name":"Alice","age":40}

Conclusion

In this blog post, we have explored how to read, write, and update JSON data in Go using code examples and explanations. By leveraging the ‘encoding/json’ package, developers can easily work with JSON data in their Go applications, allowing for seamless integration with external APIs and data sources. This knowledge is essential for anyone working with JSON data in Go projects.

This post is licensed under CC BY 4.0 by the author.
Contents