JIYIK CN >

Current Location:Home > Learning > PROGRAM > Go >

Convert JSON to struct in Go

Author:JIYIK Last Updated:2025/04/15 Views:

This article describes how to convert JSON to struct in GoLang .


Convert JSON to Struct using Unmarshal method in Go

The encoding/json package of the Go language provides a function Unmarshalto convert JSON data into byte format. This function can parse structured and unstructured JSON data into a byte []byteformat.

We can use this byte conversion to convert JSON to a struct. Let's try an example of converting JSON to a struct using the Unmarshal method:

package main

import (
    "encoding/json"
    "fmt"
)

type Employee struct {
    Name     string
    ID       int
    Salary   int
    Position string
}

func main() {
    EmployeeJSON := `{"Name": "Sheeraz", "ID": 10, "Salary": 3000, "Position": "Senior Developer" }`

    var employees Employee

    json.Unmarshal([]byte(EmployeeJSON), &employees)

    fmt.Printf("Name: %s, ID: %d , Salary: %d , Position: %s", employees.Name, employees.ID, employees.Salary, employees.Position)

    fmt.Println("\n", employees)
}

The above code uses the Unmarshal method of GoLang's JSON standard library to convert the given JSON into a structure. See the output:

Name: Sheeraz, ID: 10 , Salary: 3000 , Position: Senior Developer

{Sheeraz 10 3000 Senior Developer}

Convert JSON array to struct in Go

As we can see, the above example converts a single JSON to a struct, but what if we have an array of JSON? Let's try an example that converts a JSON array to a struct:

package main

import (
    "encoding/json"
    "fmt"
)

type Employee struct {
    Name     string
    ID       int
    Salary   int
    Position string
}

func main() {
    EmployeeJSON := `[{"Name": "Sheeraz", "ID": 10, "Salary": 3000, "Position": "Senior Developer" },
    {"Name": "Jack", "ID": 20, "Salary": 2000, "Position": "Junior Developer" },
    {"Name": "John", "ID": 30, "Salary": 1500, "Position": "Junior Developer" },
    {"Name": "Mike", "ID": 40, "Salary": 1000, "Position": "Intern" }
    ]`

    var employees []Employee

    json.Unmarshal([]byte(EmployeeJSON), &employees)

    fmt.Printf("Employees Structs : %+v", employees)
}

The above code will convert the JSON array into a structure array. See the output:

Employees Structs : [
{Name:Sheeraz ID:10 Salary:3000 Position:Senior Developer}
{Name:Jack ID:20 Salary:2000 Position:Junior Developer}
{Name:John ID:30 Salary:1500 Position:Junior Developer}
{Name:Mike ID:40 Salary:1000 Position:Intern}]
Program exited.

Convert nested JSON to struct in Go

JSON is also created in nested form where one field in JSON contains another field. To convert this type of JSONS we have to create nested structure.

Let’s look at an example:

package main

import (
    "encoding/json"
    "fmt"
)

type EmployeeType struct {
    Gender   string
    Contract string
}

type Employee struct {
    Name         string
    ID           int
    Salary       int
    Position     string
    EmployeeType EmployeeType
}

func main() {
    EmployeeJSON := `{"Name": "Sheeraz", "ID": 10, "Salary": 3000, "Position": "Senior Developer", "EmployeeType": {"Gender": "Male" ,"Contract": "Fulltime"}  }`

    var employees Employee

    json.Unmarshal([]byte(EmployeeJSON), &employees)

    fmt.Printf("Name: %s, ID: %d , Salary: %d , Position: %s, Gender: %s, Contract: %s",
        employees.Name, employees.ID, employees.Salary, employees.Position, employees.EmployeeType.Gender, employees.EmployeeType.Contract)

    fmt.Println("\n", employees)
}

The above code will convert the nested JSON object into a nested structure. See the output:

Name: Sheeraz, ID: 10 , Salary: 3000 , Position: Senior Developer, Gender: Male, Contract: Fulltime
{Sheeraz 10 3000 Senior Developer {Male Fulltime}}

Program exited.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Getting a string representation of a structure in Go

Publish Date:2025/04/15 Views:63 Category:Go

Go allows us to serialize data from structures using a variety of simple standard methods. Converting a structure to a string using String method in Go The GoLang package String helps implement simple functions to manipulate and edit UTF-8

Golang 中的零值 Nil

Publish Date:2023/04/27 Views:185 Category:Go

本篇文章介绍 nil 在 Golang 中的含义,nil 是 Go 编程语言中的零值,是众所周知且重要的预定义标识符。

Golang 中的 Lambda 表达式

Publish Date:2023/04/27 Views:699 Category:Go

本篇文章介绍如何在 Golang 中创建 lambda 表达式。Lambda 表达式似乎不存在于 Golang 中。 函数文字、lambda 函数或闭包是匿名函数的另一个名称。

Go 中的深度复制

Publish Date:2023/04/27 Views:136 Category:Go

当我们尝试生成对象的副本时,深层副本会准确复制原始对象的所有字段。 此外,如果它有任何对象作为字段,也会制作这些对象的副本。本篇文章介绍如何在 Golang 中进行深度复制。

在 Go 中捕获 Panics

Publish Date:2023/04/27 Views:104 Category:Go

像错误一样,Panic 发生在运行时。 换句话说,当您的 Go 程序中出现意外情况导致执行终止时,就会发生 Panics。让我们看一些例子来捕捉 Golang 中的Panics。

Go 中的日志级别

Publish Date:2023/04/27 Views:589 Category:Go

本篇文章介绍如何在 Golang 中创建和使用日志级别。Go 中的日志级别。Golang提供了一个日志包,名为log,是一个简单的日志包。 这个包不提供分级日志; 如果我们想要分级日志记录,我们必须

在 Go 中使用断言

Publish Date:2023/04/27 Views:593 Category:Go

本篇文章介绍了 assert 在 GoLang 中的使用。在 Go 语言中使用断言:GoLang 不提供对断言的任何内置支持,但我们可以使用来自 Testify API 的广泛使用的第三方包断言。

Go 中的随机数生成

Publish Date:2023/04/27 Views:207 Category:Go

本篇文章介绍如何在 Go 语言中使用随机数生成功能。Go 中的随机数生成 Go 语言为随机数生成功能提供内置支持。 内置包 math 有方法 rand(),用于随机数生成。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial