Basics of JSON with GoLang


JSON has grown in popularity as a data format for transferring data across apps in recent years. JSON (JavaScript Object Notation) is a compact and simple-to-understand standard for exchanging data. It is widely used in server-to-server communication, mobile applications, and web applications.

Its simplicity, concurrency, and scalability make GoLang a powerful programming language. The fundamentals of JSON with GoLang will be covered in this post.

What is JSON?

JSON is a simple, lightweight data exchange format that is simple for both people and robots to comprehend and generate. It is a completely language-independent text format that adheres to principles that are well-known to programmers of the C family of languages.

Objects and arrays are the two major types of structures in JSON. Objects are unordered collections of key-value pairs with strings as the keys and any JSON data type as the values. Arrays are collections of values in an ordered fashion that can contain any acceptable JSON data type.

Encoding JSON with GoLang

We can encode and decode JSON data using the built-in "encoding/json" package that GoLang offers. We must build a struct that maps to the JSON object in order to encode GoLang data into JSON format.

Consider a struct called "Person" as an illustration. It has two fields: "Name" and "Age."

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

To encode this struct into JSON format, we can use the "json.Marshal" function.

Example

package main

import (
   "encoding/json"
   "fmt"
)

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

func main() {
   p := Person{Name: "Asmitha", Age: 25}
   b, err := json.Marshal(p)
   if err != nil {
      fmt.Println("error:", err)
   }
   fmt.Println(string(b))
}

Output

{"name":"Asmitha","age":25}

Decoding JSON with GoLang

With the "json.Unmarshal" function, we can convert JSON data into GoLang format. This function requires a pointer to a struct to which we want to map the JSON object and a byte slice holding the JSON data.

Consider a JSON string that represents an entity called "Person," for instance.

jsonStr := `{"name":"Asmitha","age":25}`

To decode this JSON string into a "Person" struct, we can use the following code −

Example

package main

import (
   "encoding/json"
   "fmt"
)

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

func main() {
   var p Person
   jsonStr := `{"name":"Asmitha", "age":25}`
   err := json.Unmarshal([]byte(jsonStr), &p)
   if err != nil {
      fmt.Println("error:", err)
   }
   fmt.Println(p.Name, p.Age)
}

Output

Asmitha 25

Working with JSON Arrays

An array in JSON is a set of values that are organised. Slices can be used in GoLang to represent JSON arrays.

Let's use a JSON array of numbers as an illustration.

jsonStr := `[1, 2, 3, 4, 5]`

To decode this JSON array into a slice of integers, we can use the following code −

Example

package main

import (
   "encoding/json"
   "fmt"
)

func main() {
   jsonStr := "[1, 2, 3, 4, 5]"
   var numbers []int
   err := json.Unmarshal([]byte(jsonStr), &numbers)
   if err != nil {
      fmt.Println("error:", err)
   }
   fmt.Println(numbers)
}

Output

[1 2 3 4 5]

Conclusion

JSON is a popular data exchange format, and GoLang offers a straightforward and effective way to work with JSON data. In this article, we went over the fundamentals of using GoLang to encode and decode JSON data as well as how to work with JSON arrays. Knowing this information will make it simple for you to communicate and integrate JSON into your GoLang applications.

Updated on: 06-Apr-2023

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements