Use structs with proper JSON tags for predictable data to ensure fast, safe parsing with compile-time type safety. 2. Avoid map[string]interface{} due to reflection overhead and runtime type assertions unless dealing with truly dynamic JSON. 3. Use json.RawMessage for deferred or selective parsing to save CPU and memory by parsing conditionally. 4. Stream large JSON with json.Decoder and json.Encoder to process data incrementally and reduce memory usage. 5. Pre-allocate slices and reuse buffers to minimize allocations and reduce garbage collection pressure. 6. Handle unknown fields with Decoder.DisallowUnknownFields() to enforce strict validation in APIs. Efficient JSON handling in Go relies on using structs, streaming for large data, delaying parsing when possible, and minimizing allocations to improve performance and maintainability.
When working with JSON in Go, efficiency and clarity are key—especially in services that handle large payloads or high throughput. Go’s encoding/json
package provides robust tools for parsing and manipulating JSON, but knowing how to use it effectively can make a big difference in performance and code maintainability.

Here’s how to work with JSON efficiently in Go.
1. Use Structs with Proper Tags for Predictable Data
If you know the structure of your JSON, define a Go struct with json
tags. This is faster and safer than using map[string]interface{}
.

type User struct { ID int `json:"id"` Name string `json:"name"` Email string `json:"email,omitempty"` }
json:"name"
maps the field to the JSON key.omitempty
omits the field from output if it's empty (zero value).
Parsing becomes straightforward:
var user User err := json.Unmarshal(data, &user) if err != nil { log.Fatal(err) }
? Why it's efficient: Direct memory allocation, compile-time type safety, and no runtime type assertions.

2. Avoid map[string]interface{}
When Possible
While convenient, untyped maps are slow and error-prone:
var data map[string]interface{} json.Unmarshal(rawJSON, &data) name := data["name"].(string) // Panic if type is wrong
? Problems:
- Reflection overhead during parsing.
- Type assertions required.
- No compile-time checks.
Only use map[string]interface{}
for truly dynamic or unknown JSON (e.g., configuration with variable keys).
For semi-structured data, consider using json.RawMessage
to defer parsing.
3. Use json.RawMessage
for Deferred or Selective Parsing
json.RawMessage
lets you store raw JSON data as a byte slice, delaying parsing until needed.
type Message struct { Type string `json:"type"` Payload json.RawMessage `json:"payload"` } var msg Message json.Unmarshal(data, &msg) // Now parse payload based on type switch msg.Type { case "user": var user User json.Unmarshal(msg.Payload, &user) case "event": var event Event json.Unmarshal(msg.Payload, &event) }
? Efficiency win: Avoids parsing unused or conditional data upfront.
4. Stream Large JSON with json.Decoder
and json.Encoder
For large JSON files or HTTP streams, use json.Decoder
and json.Encoder
to avoid loading everything into memory.
Reading from a file or HTTP body:
decoder := json.NewDecoder(r.Body) for { var user User if err := decoder.Decode(&user); err == io.EOF { break } else if err != nil { log.Fatal(err) } // Process user one at a time }
Writing streaming JSON:
encoder := json.NewEncoder(w) for _, user := range users { encoder.Encode(user) // Writes directly to output }
? Ideal for large datasets, APIs, or log processors.
5. Pre-allocate Slices and Reuse Buffers
If parsing many JSON objects, reduce allocations:
- Pre-allocate slices when size is known.
- Reuse
*json.Decoder
and buffers in loops.
Example:
users := make([]User, 0, 1000) // Pre-allocate capacity
For high-performance scenarios, consider pooling buffers:
var bufPool = sync.Pool{ New: func() interface{} { return new(bytes.Buffer) }, }
Though json.Unmarshal
is usually fast enough, reducing GC pressure matters at scale.
6. Handle Unknown Fields Gracefully
By default, unknown JSON fields are ignored. To catch them (e.g., for validation), use Decoder.DisallowUnknownFields()
:
decoder := json.NewDecoder(r.Body) decoder.DisallowUnknownFields() err := decoder.Decode(&user) // Returns error if JSON contains unexpected keys
Useful in APIs to reject malformed or malicious input.
Summary
Technique | Best For | Efficiency Benefit |
---|---|---|
Structs with JSON tags | Known schema | Fast, safe, readable |
json.RawMessage |
Conditional or delayed parsing | Saves CPU and memory |
json.Decoder /Encoder
|
Large or streaming data | Low memory, ideal for I/O |
Avoid interface{}
|
Performance-critical code | Reduces reflection and errors |
Pre-allocation & pooling | High-throughput services | Reduces GC pressure |
Efficient JSON handling in Go isn’t about avoiding the standard library—it’s about using it wisely. Stick to structs when possible, stream when needed, and delay parsing selectively.
Basically, know your data shape, minimize allocations, and let the encoding/json
package do the heavy lifting—just guide it well.
The above is the detailed content of Efficient JSON Parsing and Manipulation in Go. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go's switch statement will not be executed throughout the process by default and will automatically exit after matching the first condition. 1. Switch starts with a keyword and can carry one or no value; 2. Case matches from top to bottom in order, only the first match is run; 3. Multiple conditions can be listed by commas to match the same case; 4. There is no need to manually add break, but can be forced through; 5.default is used for unmatched cases, usually placed at the end.

Usecontexttopropagatecancellationanddeadlinesacrossgoroutines,enablingcooperativecancellationinHTTPservers,backgroundtasks,andchainedcalls.2.Withcontext.WithCancel(),createacancellablecontextandcallcancel()tosignaltermination,alwaysdeferringcancel()t

Use a dedicated and reasonably configured HTTP client to set timeout and connection pools to improve performance and resource utilization; 2. Implement a retry mechanism with exponential backoff and jitter, only retry for 5xx, network errors and 429 status codes, and comply with Retry-After headers; 3. Use caches for static data such as user information (such as sync.Map or Redis), set reasonable TTL to avoid repeated requests; 4. Use semaphore or rate.Limiter to limit concurrency and request rates to prevent current limit or blocking; 5. Encapsulate the API as an interface to facilitate testing, mocking, and adding logs, tracking and other middleware; 6. Monitor request duration, error rate, status code and retry times through structured logs and indicators, combined with Op

To correctly copy slices in Go, you must create a new underlying array instead of directly assigning values; 1. Use make and copy functions: dst:=make([]T,len(src));copy(dst,src); 2. Use append and nil slices: dst:=append([]T(nil),src...); both methods can realize element-level copying, avoid sharing the underlying array, and ensure that modifications do not affect each other. Direct assignment of dst=src will cause both to refer to the same array and are not real copying.

Go uses time.Time structure to process dates and times, 1. Format and parse the reference time "2006-01-0215:04:05" corresponding to "MonJan215:04:05MST2006", 2. Use time.Date(year, month, day, hour, min, sec, nsec, loc) to create the date and specify the time zone such as time.UTC, 3. Time zone processing uses time.LoadLocation to load the position and use time.ParseInLocation to parse the time with time zone, 4. Time operation uses Add, AddDate and Sub methods to add and subtract and calculate the interval.

Use the template.ParseFS and embed package to compile HTML templates into binary files. 1. Import the embed package and embed the template file into the embed.FS variable with //go:embedtemplates/.html; 2. Call template.Must(template.ParseFS(templateFS,"templates/.html")))) to parse all matching template files; 3. Render the specified in the HTTP processor through tmpl.ExecuteTemplate(w,"home.html", nil)

To import local packages correctly, you need to use the Go module and follow the principle of matching directory structure with import paths. 1. Use gomodinit to initialize the module, such as gomodinitexample.com/myproject; 2. Place the local package in a subdirectory, such as mypkg/utils.go, and the package is declared as packagemypkg; 3. Import it in main.go through the full module path, such as import "example.com/myproject/mypkg"; 4. Avoid relative import, path mismatch or naming conflicts; 5. Use replace directive for packages outside the module. Just make sure the module is initialized

ToformatJSONinSublimeText,firstensurethefile'ssyntaxissettoJSONviaView→Syntax→JSON.2.Usethebuilt-inreindentcommandwithCtrl Alt B(Windows/Linux)orCmd Ctrl B(Mac)toformattheJSON.3.Forenhancedfeatureslikevalidationandsorting,installthePrettyJSONpluginvi
