Go JSON marshaling and unmarshaling¶
In Go programming, exchanging data with external systems or files often requires converting internal data structures (like structs) into formats such as JSON, XML, or gRPC.^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md] The encoding/json package in the standard library provides the functionality to convert between Go types and JSON, allowing applications to interact with APIs or databases.^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md]
Unmarshaling¶
Unmarshaling is the process of converting JSON data (specifically bytes) into a Go data structure, such as a struct or slice^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md].
This is commonly done when reading a file (e.g., using ioutil.ReadFile), which returns a byte slice.^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md] The json.Unmarshal function is then used to parse these bytes into the target variable^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md]. The function requires a byte slice input and a pointer to the variable where the data should be stored^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md].
fileBytes, err := ioutil.ReadFile("./videos.json")
// Check err...
err = json.Unmarshal(fileBytes, &videos)
Marshaling¶
Marshaling is the reverse process: converting a Go data structure (like a struct) into JSON bytes^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md].
This is typically used when preparing data to be sent over a network or saved to a file^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md]. The json.Marshal function takes the Go object (e.g., a slice of structs) and returns a byte slice of the JSON representation^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md]. These bytes can then be written to disk using functions like ioutil.WriteFile^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md].
videoBytes, err := json.Marshal(videos)
// Check err...
ioutil.WriteFile("./videos-updated.json", videoBytes, 0644)
Mapping fields¶
Go allows developers to explicitly map JSON keys to specific struct fields using struct tags^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md].
While struct fields and JSON keys may match by default if they are identically named and exported, tags allow for mapping keys that differ in naming convention (e.g., camelCase JSON keys to PascalCase struct fields)^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md]. This is done by adding a string literal annotation immediately after the field declaration^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md].
type video struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Imageurl string `json:"imageurl"`
Url string `json:"url"`
}
Related¶
- [[Go struct]]
- I/O in Go
Sources¶
^[400-devops-09-scripting-language-golang-introduction-part-2json-readme.md]