Golang program of two struct types with identical fields, with having one "JSON" tag on each field


Struct types are used to construct custom data structures consist of fields. These fields reflect the structure's characteristics or attributes. The ability to add tags to fields, such as "JSON" tags, which offer additional metadata about the field when serializing or deserializing the struct to/from JSON format, is a valuable feature of struct types.

In this article, we will write Go language programs to represent structs with one JSON tag in each field.

type StructName struct {
    Field1 DataType1 `Tag1:"Value1" Tag2:"Value2"`
    Field2 DataType2 `Tag3:"Value3" Tag4:"Value4"`
    // ...
    FieldN DataTypeN `TagN:"ValueN"`
}
  • StructName − Name of the type of the structure.

  • FieldName − It is the name of the field.

  • DataType − It is the type of the data for the field.

  • Tags are always enclosed in (``) backticks, and have the format TagKey:”TagValue”.

Algorithm

  • Step 1 − This program imports “encoding/json” and “fmt” as necessary packages

  • Step 2 − Create two structs named Person and Employee where these structs have fields Name, Age, and Address, with corresponding json tags

  • Step 3 − Create a main function

  • Step 4 − In the main, create an object of Person struct named person and initialize its fields with some values

  • Step 5 − In this step, create an object of the Employee struct named employee and initialize its fields with some values

  • Step 6 − Then, use json.Marshal() method to convert the person and employee structs to JSON format

  • Step 7 − Store the output obtained in personJSON and employeeJSON variables

  • Step 8 − Finally, print the JSON representation of the person and employee structs using fmt.Println().

Example

In this example, we will write a Go language program to represent two structs named Person and Employee with JSON tag on each field.

package main

import (
	"encoding/json"
	"fmt"
)

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

type Employee struct {
	Name    string `json:"emp_name"`
	Age     int    `json:"emp_age"`
	Address string `json:"emp_address"`
}

func main() {
	person := Person{
		Name:    "Karan",
		Age:     24,
		Address: "Hauz Khas",
	}

	employee := Employee{
		Name:    "Tarun",
		Age:     26,
		Address: "Saket",
	}

	personJSON, _ := json.Marshal(person)
	employeeJSON, _ := json.Marshal(employee)

	fmt.Println("Person JSON:", string(personJSON))
	fmt.Println("Employee JSON:", string(employeeJSON))
}

Output

Person JSON: {"name":"Karan","age":24,"address":"Hauz Khas"}
Employee JSON: {"emp_name":"Tarun","emp_age":26,"emp_address":"Saket"}

Example

In this example, we will write a Golang program to represent two structs named Car and Vehicle with a JSON tag on each field.

package main

import (
	"encoding/json"
	"fmt"
)

type Car struct {
	Brand  string `json:"brand"`
	Model  string `json:"model"`
	Year   int    `json:"year"`
	Engine string `json:"engine"`
}

type Vehicle struct {
	Brand  string `json:"manufacturer"`
	Model  string `json:"model"`
	Year   int    `json:"production_year"`
	Engine string `json:"engine_type"`
}

func main() {
	car := Car{
		Brand:  "Hyundai",
		Model:  "Camry",
		Year:   2022,
		Engine: "V6",
	}

	vehicle := Vehicle{
		Brand:  "Honda",
		Model:  "Civic",
		Year:   2022,
		Engine: "Inline-4",
	}

	carJSON, _ := json.Marshal(car)
	vehicleJSON, _ := json.Marshal(vehicle)

	fmt.Println("Car JSON:", string(carJSON))
	fmt.Println("Vehicle JSON:", string(vehicleJSON))
}

Output

Car JSON: {"brand":"Hyundai","model":"Camry","year":2022,"engine":"V6"}
Vehicle JSON: {"manufacturer":"Honda","model":"Civic","production_year":2022,"engine_type":"Inline-4"}

Conclusion

In this article, we discussed how we can write a program to represent struct with one JSON tag in each field. We compiled and executed the program of having structs with JSON tags on each field using two examples. In the first example, we used Person and Employee struct and in the second example, we used Car and vehicle struct.

Updated on: 06-Jul-2023

705 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements