Golang Struct JSON Tutorial - Convert Go Struct to / from JSON

In this tutorial, we will learn how to use JSON in Golang with examples.

JSON (JavaScript Object Notation) is a lightweight data interchange format.

In Golang, the encoding/json package implements the encoding and decoding of JSON.

Table of contents:

  • Go - Convert Struct to JSON
  • Go - Convert an array/slice into a JSON
  • Go - Convert JSON to Struct
  • Go - Convert JSON to an array/slice

Go - Convert Struct to JSON

In the example, we transform a Go struct into JSON format:

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type Employee struct {
    Id         int
    FirstName       string
    LastName string
    Email string
}

func main() {

    e1 := Employee{1, "John", "Cena", "[email protected]"}

    json_data, err := json.Marshal(e1)

    if err != nil {

        log.Fatal(err)
    }

    fmt.Println(string(json_data))
}

Output:

{"Id":1,"FirstName":"John","LastName":"Cena","Email":"[email protected]"}

We declare the Employee struct:

type Employee struct {
    Id         int
    FirstName       string
    LastName string
    Email string
}

We create the struct instance:

    e1 := Employee{1, "John", "Cena", "[email protected]"}

We encode the u1 struct into JSON with Marshal.

json_data, err := json.Marshal(e1)

Since json_data is a byte array, we convert it to a string with the string function:

fmt.Println(string(json_data))

Go - Convert an array/slice into a JSON

In the example, we transform a slice of structs into JSON format:

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type Employee struct {
    Id         int
    FirstName       string
    LastName string
	Email string
}

func main() {

    employees := []Employee{
        {Id: 2, FirstName: "Amir", LastName: "Khan", Email: "[email protected]"},
        {Id: 3, FirstName: "Lucy", LastName: "Smith", Email: "[email protected]"},
        {Id: 4, FirstName: "David", LastName: "Brown", Email: "[email protected]"},
    }

    json_data2, err := json.Marshal(employees)

    if err != nil {

        log.Fatal(err)
    }

    fmt.Println(string(json_data2))
}

Output:

[{"Id":2,"FirstName":"Amir","LastName":"Khan","Email":"[email protected]"},{"Id":3,"FirstName":"Lucy","LastName":"Smith","Email":"[email protected]"},{"Id":4,"FirstName":"David","LastName":"Brown","Email":"[email protected]"}]

The output can be pretty-printed with the MarshalIndent function.

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type Employee struct {
    Id         int
    FirstName       string
    LastName string
	Email string
}

func main() {

    employees := []Employee{
        {Id: 2, FirstName: "Amir", LastName: "Khan", Email: "[email protected]"},
        {Id: 3, FirstName: "Lucy", LastName: "Smith", Email: "[email protected]"},
        {Id: 4, FirstName: "David", LastName: "Brown", Email: "[email protected]"},
    }

    json_data2, err := json.MarshalIndent(employees, "", "	")

    if err != nil {

        log.Fatal(err)
    }

    fmt.Println(string(json_data2))
}

Output:

[
	{
		"Id": 2,
		"FirstName": "Amir",
		"LastName": "Khan",
		"Email": "[email protected]"
	},
	{
		"Id": 3,
		"FirstName": "Lucy",
		"LastName": "Smith",
		"Email": "[email protected]"
	},
	{
		"Id": 4,
		"FirstName": "David",
		"LastName": "Brown",
		"Email": "[email protected]"
	}
]

Go - Convert JSON to Struct

In this source code example, we show how to convert JSON to Struct in Golang :

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type Employee struct {
    Id         int
    FirstName       string
    LastName string
	Email string
}

func main() {

    var e1 Employee

    data := []byte(`{"Id": 1, "FirstName": "John", "LastName": "Cena", "Email" : "[email protected]"}`)

    err := json.Unmarshal(data, &e1)

    if err != nil {

        log.Fatal(err)
    }

    fmt.Println("Struct is:", e1)
    fmt.Println("FirstName: ", e1.FirstName)
    fmt.Println("LastName: ", e1.LastName)
    fmt.Println("Email: ", e1.Email)
}

Output:

Struct is: {1 John Cena [email protected]}
FirstName:  John
LastName:  Cena
Email:  [email protected]

Go - Convert JSON to an array/slice

In this source code example, we show how to convert JSON to an array/slice in Golang with an example:

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type Employee struct {
    Id         int
    FirstName       string
    LastName string
	Email string
}

func main() {

    var employees []Employee

    data2 := []byte(`
    [
        {"Id": 1, "FirstName": "Amir", "LastName": "Khan", "Email": "[email protected]"},
        {"Id": 2, "FirstName": "Lucy", "LastName": "Smith", "Email": "[email protected]"},
        {"Id": 3, "FirstName": "David", "LastName": "Brown", "Email": "[email protected]"}
    ]`)

    err2 := json.Unmarshal(data2, &employees)

    if err2 != nil {

        log.Fatal(err2)
    }

    for i := range employees {

        fmt.Println(employees[i])
    }
}

Output:

{1 Amir Khan [email protected]}
{2 Lucy Smith [email protected]}
{3 David Brown [email protected]}

Golang Related Tutorials

Comments