Comparison of Go JSON Parsing Libraries: Performance, Features, and Usage Guidelines
The article compares Go's built‑in encoding/json with third‑party libraries jsoniter, easyjson, jsonparser, and the author's jsonvalue, outlining each one's features, performance benchmarks, and ideal use cases—standard library for simplicity, easyjson for maximum speed with code generation, jsonparser or jsoniter for selective extraction, and jsonvalue for flexible, case‑insensitive handling.
This article examines the limitations of Go's standard encoding/json library and evaluates several third‑party JSON parsing libraries for Go, including jsoniter, easyjson, jsonparser, and the author's own jsonvalue. It discusses when to use each library, their performance characteristics, and practical usage patterns.
1. Standard library ( encoding/json )
The built‑in library can unmarshal JSON into structs or map[string]interface{}. It also handles case‑insensitive field matching and supports all JSON types (object, array, string, number, boolean, null). Example:
var s string
err := json.Unmarshal([]byte(`"Hello, world!"`), &s)Case‑insensitive example:
type Cert struct {
Username string `json:"username"`
Password string `json:"password"`
}
var cert Cert
err := json.Unmarshal([]byte(`{"UserName":"root","passWord":"123456"}`), &cert)2. jsoniter
Claims high performance and full compatibility with the standard library. It offers on‑demand parsing via jsoniter.Get and can retrieve values without full unmarshalling. Example:
username := jsoniter.Get(data, "response", "userList", 0, "name")
fmt.Println("username:", username.ToString())Performance: ~1.4× faster than the standard library for struct unmarshalling, but slower than easyjson.
3. easyjson
Generates custom marshal/unmarshal code per struct (similar to protobuf), achieving the best performance (2.5–3× faster than others). However, it requires a code generation step, increasing build complexity.
4. jsonparser
Provides low‑level parsing functions like ArrayEach and ObjectEach that return raw byte slices for further processing. It excels in shallow parsing and high‑throughput scenarios but offers limited high‑level API.
Example of extracting a string:
username, err := jsonparser.GetString(data, "response", "userList", "[0]", "name")
if err != nil { /* handle */ }
fmt.Println("username:", username)5. jsonvalue (author's library)
Combines on‑demand parsing with a convenient API similar to jsoniter. Supports case‑insensitive lookups via Caseless() and can build JSON objects programmatically without reflection:
res := jsonvalue.NewObject()
res.SetInt(0).At("code")
res.SetString("success").At("message")
res.SetString(nickname).At("data", "nickname")
b := res.MustMarshal()6. Benchmark results
Benchmarks show easyjson leads in pure struct marshal/unmarshal speed, jsoniter slightly outperforms the standard library, and jsonparser dominates in shallow parsing and selective field extraction. For full JSON processing where both performance and ease of use matter, jsonvalue offers a balanced solution.
7. Recommendations
For typical struct serialization/deserialization, prefer the standard library for simplicity and compatibility.
When maximum speed is required and code generation is acceptable, use easyjson.
For selective field extraction without full unmarshalling, choose jsonparser (best performance) or jsoniter (more convenient API).
When handling non‑structured JSON with frequent modifications or need case‑insensitive access, jsonvalue is recommended.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
