Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

JSON Marshalling & Unmarsha | Go Programming Bootcamp lesson | BraveProgrammer
  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 19 of 60

·Go

JSON Marshalling & Unmarshalling

20 minutes

Course progress32%

JSON in Go

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

The encoding/json package provides Marshal and Unmarshal.

type User struct {
    ID   int    `json:"id"`
    Name string 
    Age      
}

u := User{ID: , Name: }
bytes, _ := json.Marshal(u)
fmt.Println((bytes)) 
`json:"name"`
int
`json:"age,omitempty"`
1
"Alice"
string
// {"id":1,"name":"Alice"}

Decoding from a request body

func createUser(w http.ResponseWriter, r *http.Request) {
    var u User
    if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    // process u …
    w.WriteHeader(http.StatusCreated)
}

Custom Marshal/Unmarshal

Implement the json.Marshaler and json.Unmarshaler interfaces for special handling.

Info

Struct tags can control field names, omitempty, and more.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from Go Programming BootcampBrowse activities
Previous lessonStandard Library: `net/http` – Building a Simple Web ServerContinue to lesson 20Template Rendering with `html/template`Back to Go Programming Bootcamp curriculum