0Pricing
Go Academy · Lesson

encoding/json Recap

Marshal and unmarshal JSON.

encoding/json Recap is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

JSON in Go

JSON is the most common data format for APIs. Go's standard encoding/json package converts between Go values and JSON text.

Marshal: Go to JSON

json.Marshal turns a Go value into a JSON byte slice. Convert it to a string to print.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	data := map[string]int{"a": 1, "b": 2}
	b, _ := json.Marshal(data)
	fmt.Println(string(b))
}

Marshaling a Struct

Structs marshal field by field. Exported fields become JSON keys.

package main

import (
	"encoding/json"
	"fmt"
)

type User struct {
	Name string
	Age  int
}

func main() {
	b, _ := json.Marshal(User{"Ann", 30})
	fmt.Println(string(b))
}

Unmarshal: JSON to Go

json.Unmarshal parses JSON bytes into a Go value. Pass a pointer so it can fill your variable.

package main

import (
	"encoding/json"
	"fmt"
)

type User struct {
	Name string
	Age  int
}

func main() {
	var u User
	json.Unmarshal([]byte("{\"Name\":\"Bob\",\"Age\":25}"), &u)
	fmt.Println(u)
}

Tags for Key Names

Use json tags to control key names, as covered earlier. Here Name maps to the JSON key name.

package main

import (
	"encoding/json"
	"fmt"
)

type User struct {
	Name string "json:\"name\""
}

func main() {
	b, _ := json.Marshal(User{"Ann"})
	fmt.Println(string(b))
}

Always Check Errors

Both Marshal and Unmarshal return an error. In real code, never ignore it; malformed JSON or unsupported types produce errors.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	var v int
	err := json.Unmarshal([]byte("not json"), &v)
	fmt.Println(err)
}

Decoding into Maps

When you do not know the shape ahead of time, unmarshal into map[string]interface{}.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	var m map[string]interface{}
	json.Unmarshal([]byte("{\"x\":1,\"y\":\"hi\"}"), &m)
	fmt.Println(m["x"], m["y"])
}

Slices and Arrays

JSON arrays map to Go slices. Marshaling a slice yields a JSON array.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	b, _ := json.Marshal([]string{"a", "b", "c"})
	fmt.Println(string(b))
}

Pretty Printing

json.MarshalIndent adds indentation for human-readable output.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	data := map[string]int{"a": 1}
	b, _ := json.MarshalIndent(data, "", "  ")
	fmt.Println(string(b))
}

Numbers Are float64

When decoding into interface{}, all JSON numbers become float64 in Go. Convert explicitly if you need an int.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	var v interface{}
	json.Unmarshal([]byte("42"), &v)
	fmt.Printf("%T %v\n", v, v)
}

Round Trip

You can marshal then unmarshal to copy data through JSON. The values come back intact for supported types.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	orig := []int{1, 2, 3}
	b, _ := json.Marshal(orig)
	var back []int
	json.Unmarshal(b, &back)
	fmt.Println(back)
}

Quick Check

Why must you pass a pointer to json.Unmarshal?

Recap

encoding/json:

  • json.Marshal turns Go values into JSON
  • json.Unmarshal parses JSON into a pointer
  • Tags rename keys; numbers decode as float64 into interface{}
  • Always check the returned error

Frequently asked questions

Is the “encoding/json Recap” lesson free?

Yes — the full text of “encoding/json Recap” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “encoding/json Recap”?

Marshal and unmarshal JSON. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “encoding/json Recap” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. encoding/json Recap
  2. encoding/xml
  3. encoding/csv
  4. gob and Binary Encoding
← Back to Go Academy