Updated April 13, 2023
Definition of Golang JSON
In GO language JSON (Javascript Object Notation)is an import data storage strategy, it allows us to store data in the key-value pair, in web development JSON is the most important data which is very lightweight and flexible, as we know on the browser JSON is the data which is mostly supported, in go language we can define the attribute of the JSON with the data type of each attribute, for example, if we are creating a JSON of the student then we can place student name as the string type, similarly, if we want to put the student age also in the JSON then we can place with int as the data type of the attribute.
Syntax of Golang JSON
Below is a simple example for the JSON data for the go language, we can explain the below syntax in the following steps.
- name_of_storage: This attribute is the name of the store where we are going to place various types of the data, for example, if we are creating storage for students then we can name it as the Student. It can hold the mixed type of data for example we can store data of string for the name of the student and integer data for the age of the students.
- struct: This is a keyword that notifies the go compiler about the structure of the data.
- Attribute1 and Attribute2: These are the attributes of the storage, for example, if we are creating storage JSON for the student data then we can say Attribute1 and Attribute2 are the two data for the student’s name and student age.
- data-type: This used to define the datatype of Attribute1 and Attribute2.For example, if we are going to store the string name of the student then the data type will be a string and if we are going to store student age then data type will be an integer.
Please see the below syntax for better understanding.
type name_of_storage struct {
Attribute1 data-type `json:"section_name"`
Attribute2 data-type `json:"class_name"`
}
How does JSON Work in Go Language?
Before we discuss the working of the JSON in the Go language let us focus on little uses and purposes of the JSON.JSON means JavascriptObject Notation, here we are calling it the object because everything inside the JSON data is like any object. If you have seen our application so in the current days every web application and android apps use JSON data only because it is very lightweight and easy to use. It allows us to store mixed types of data inside it, for example, we can put string, integer, boolean, and float types data in the given storage. Let us discuss some of the key points to reveal the working of JSON in the Go language.
- Here we need to convert our object generated by us into JSON with the help of the marshaling effect, Marshal is a function that will take an object and will return as the JSON of the respective object.
- First, before using the JSON in the go language, we need to use the encoding/json data type, it allows us to create JSON storage.
- Next, we need to define the type and name of the JSON which will be used throughout the program.
- We need to define a struct to define storage for the JSON, here struct is a keyword in the go language.
- Inside the JSON we need to define the attributes and the type for the attribute, these types can be an integer and string anything.
- Once we define the type for the attributes of the JSON storage, whoever will use this JSON they need to mention the same type of data.
- Support a defined student name attribute inside the JSOn as the string and we are trying to store the integer value then it will throw an error, which means it will only accept the string value here.
Examples of Golang JSON
Below, we have given some examples for understanding JSON in the go language. We can run them on our system, we can create a file with the name json.go and copy-paste the below example on the file and run the command go run json.go.We can give any name to the file according to our uses.
Example #1
Below is a simple example where we are showing a simple JSON converted with the Marshal function of the JSON. Here in the below example, we are combining string and numeric values for the students and the class. We have taken Student as the data type for the Class, where we can assign the student.
Code:
package main
import (
"encoding/json"
"fmt"
)
type Class struct {
Section string `json:"section_name"`
Student Student `json:"class_name"`
}
type Student struct {
Name string `json:"student_name"`
Age int `json:"student_age"`
Subject string `json:"subject_name"`
}
func main() {
student := Student{Name: "Ranjan Kumar Pandey", Age: 31, Subject: "Programing"}
class := Class{Section: "section-A", Student: student}
combinedJson, err := json.Marshal(class)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(combinedJson))
}
Output:
Example #2
Below is also a simple example, here in the below example we display JSON with a combination of the string and boolean type.
Code:
package main
import (
"encoding/json"
"fmt"
)
type Animal struct {
Animaltype string `json:"type"`
Details Details `json:"details"`
type Details struct {
Name string `json:"animal_name"`
Big bool `json:"is_Biganimal"`
Food string `json:"animal_food"`
}
func main() {
lion := Details{Name: "Lion", Big: true, Food: "Other animals"}
animal := Animal{Animaltype: "The animal is a big animal and live in jungle", Details: lion}
combinedJson, err := json.Marshal(animal)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(combinedJson))
}
Output:
Recommended Articles
This is a guide to Golang JSON. Here we also discuss the introduction, syntax, and working of JSON in the go language along with different examples and its code implementation. you may also have a look at the following articles to learn more –