How to Convert JSON to Struct in Golang

To convert a JSON string (or a byte slice containing JSON) to a Go struct, you can “use the json.Unmarshal() function from the encoding/json package.”

Go JSON to Struct

Here is the step-by-step guide to convert JSON to Struct in Go:

  1. Define the struct that matches the expected structure of the JSON data.
  2. Convert the JSON string to a byte slice (if it’s not already in that form).
  3. Use json.Unmarshal() function to decode the JSON into the struct.


package main

import (

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

func main() {
  jsonData := []byte(`{"name":"Krunal Lathiya","age":30}`)

  var person Person

  err := json.Unmarshal(jsonData, &person)
  if err != nil {

  fmt.Printf("Name: %s, Age: %d\n", person.Name, person.Age)


Name: Krunal Lathiya, Age: 30

That’s it!

Related posts

Go struct to json

Go map to struct

Go struct to string

Go interface to struct

Go string to json