How to Parse JSON in Go: Simple Guide with Examples
In Go, you parse JSON using the
encoding/json package by calling json.Unmarshal to convert JSON data into Go structs or maps. You provide the JSON bytes and a pointer to the variable where you want the data stored.Syntax
The main function to parse JSON in Go is json.Unmarshal. It takes two arguments: the JSON data as a byte slice and a pointer to the variable where the parsed data will be stored.
jsonData []byte: The raw JSON data.v interface{}: A pointer to a Go variable (usually a struct or map) to hold the parsed data.
The function returns an error if parsing fails.
go
err := json.Unmarshal(jsonData, &v) if err != nil { // handle error }
Example
This example shows how to parse a JSON string representing a person into a Go struct and print the fields.
go
package main import ( "encoding/json" "fmt" ) type Person struct { Name string `json:"name"` Age int `json:"age"` } func main() { jsonData := []byte(`{"name": "Alice", "age": 30}`) var p Person err := json.Unmarshal(jsonData, &p) if err != nil { fmt.Println("Error parsing JSON:", err) return } fmt.Println("Name:", p.Name) fmt.Println("Age:", p.Age) }
Output
Name: Alice
Age: 30
Common Pitfalls
Common mistakes when parsing JSON in Go include:
- Not using a pointer when calling
json.Unmarshal, which prevents the data from being stored. - Struct fields not being exported (starting with lowercase letters), so JSON cannot fill them.
- Missing or incorrect JSON tags on struct fields, causing mismatches.
- Ignoring errors returned by
json.Unmarshal.
go
package main import ( "encoding/json" "fmt" ) type Person struct { name string `json:"name"` // lowercase field: unexported Age int `json:"age"` } func main() { jsonData := []byte(`{"name": "Bob", "age": 25}`) var p Person // Wrong: passing value instead of pointer err := json.Unmarshal(jsonData, p) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Name:", p.name) // empty because unexported fmt.Println("Age:", p.Age) // Correct way: var p2 Person err = json.Unmarshal(jsonData, &p2) // pointer used if err != nil { fmt.Println("Error:", err) return } fmt.Println("Correct Name:", p2.name) // still empty because unexported fmt.Println("Correct Age:", p2.Age) }
Output
Name:
Age: 25
Correct Name:
Correct Age: 25
Quick Reference
| Concept | Description | Example |
|---|---|---|
| json.Unmarshal | Parse JSON bytes into Go variable | json.Unmarshal(data, &v) |
| Pointer | Pass pointer to store parsed data | &v |
| Struct Tags | Map JSON keys to struct fields | `json:"key"` |
| Exported Fields | Struct fields must start with uppercase | Name string |
| Error Handling | Always check error from Unmarshal | if err != nil {...} |
Key Takeaways
Use json.Unmarshal with a pointer to parse JSON into Go structs or maps.
Struct fields must be exported (start with uppercase) to be filled by JSON.
Add json tags to struct fields to match JSON keys exactly.
Always check the error returned by json.Unmarshal to catch parsing issues.
Passing a non-pointer or unexported fields will cause parsing to fail silently.